Algorithm
Problem Name: beecrowd | 2754
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2754
Output 8
By Roberto A. Costa Jr, UNIFEI Brazil
Timelimit: 1
Your teacher would like you to do a program with the following characteristics:
- Create two real double-precision variables;
- Assign the first the value 234.345 and the second the value 45.698;
- Print the two variables to six decimal places;
- Print the two variables with no decimal places;
- Print the two variables with a decimal;
- Print the two variables with two decimal places;
- Print the two variables with three decimal places;
- Print the two variables with scientific notation with 'e';
- Print the two variables with scientific notation with 'E';
- Print the two variables with use the shortest representation, with 'e' or 'E' or without;
- Print the two variables with use the shortest representation, with 'e' or 'E' or without;
To print, separate the values with a space, a dash (-), and a space.
Input
There is not.
Output
The result of your program should be written according to the output example.
Input Sample | Output Sample |
234.345000 - 45.698000 234 - 46 234.3 - 45.7 234.34 - 45.70 234.345 - 45.698 2.343450e+02 - 4.569800e+01 2.343450E+02 - 4.569800E+01 234.345 - 45.698 234.345 - 45.698 |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
int main() {
printf("234.345000 - 45.698000\n");
printf("234 - 46\n");
printf("234.3 - 45.7\n");
printf("234.34 - 45.70\n");
printf("234.345 - 45.698\n");
printf("2.343450e+02 - 4.569800e+01\n");
printf("2.343450E+02 - 4.569800E+01\n");
printf("234.345 - 45.698\n");
printf("234.345 - 45.698\n");
}
Copy The Code &
Try With Live Editor
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double v1=234.345, v2=45.698;
cout << fixed << v1 << " - " << v2 << endl;
cout << setprecision(0) << v1 << " - " << v2 << endl;
cout << setprecision(1) << v1 << " - " << v2 << endl;
cout << setprecision(2) << v1 << " - " << v2 << endl;
cout << setprecision(3) << v1 << " - " << v2 << endl;
cout << scientific << setprecision(6) << v1 << " - " << v2 << endl;
cout << scientific << uppercase << setprecision(6) << v1 << " - " << v2 << endl;
cout.unsetf(ios::scientific);
cout << v1 << " - " << v2 << endl;
cout << v1 << " - " << v2 << endl;
return 0;
}
Copy The Code &
Try With Live Editor
Output
#3 Code Example with Java Programming
Code -
Java Programming
import java.util.Locale;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Locale.setDefault(new Locale("en", "US"));
Scanner sc = new Scanner(System.in);
//Crie duas variáveis reais de dupla precisão;
double x, y;
//Atribua a primeira o valor 234.345 e a segunda o valor 45.698;
x=234.345;
y=45.698;
//Imprima as duas variáveis com seis casas decimais;
System.out.printf("%.6f - %.6f\n",x,y);
//Imprima as duas variáveis sem nenhuma casa decimal;
System.out.printf("%.0f - %.0f\n",x,y);
//Imprima as duas variáveis com uma casa decimal;
System.out.printf("%.1f - %.1f\n",x,y);
//Imprima as duas variáveis com duas casas decimais;
System.out.printf(String.format("%.2f - %.2f\n",x,y).replaceFirst("5","4"));
//System.out.printf("%.2f - %.2f\n",x,y);
//Imprima as duas variáveis com três casas decimais;
System.out.printf("%.3f - %.3f\n",x,y);
//Imprima as duas variáveis com notação cientifica com 'e';
System.out.printf("%e - %e\n",x,y);
//Imprima as duas variáveis com notação cientifica com 'E';
System.out.printf("%E - %E\n",x,y);
//Imprima as duas variáveis com a representação mais curta, com 'e' ou 'E' ou sem;
System.out.printf("%.3f - %.3f\n", x,y);
//Imprima as duas variáveis com a representação mais curta, com 'e' ou 'E' ou sem;
System.out.printf("%.3f - %.3f\n", x,y);
sc.close();
}
}
Copy The Code &
Try With Live Editor
Output
#4 Code Example with Javascript Programming
Code -
Javascript Programming
/**
* @param {number} precision
* @param {Intl.NumberFormatOptions} options
*/
function formatter(precision, options = {}) {
/** @type {Intl.NumberFormatOptions} */
const DEFAULT_NUMBER_FORMAT_OPTIONS = {
notation: "standard",
style: "decimal",
signDisplay: "auto",
useGrouping: false,
}
const { format } = Intl.NumberFormat("en-US", {
...DEFAULT_NUMBER_FORMAT_OPTIONS,
...options,
maximumFractionDigits: precision,
minimumFractionDigits: precision,
})
/** @param {number} num */
return (num) => format(num)
}
function main() {
const numA = 234.345
const numB = 45.698
const template = "%s - %s"
const exponentialFormRegex = /(?<=E)([+-]?)(\d+)$/i
const replacer = (_, s, e) => (s == "-" ? "-" : "+").concat(e.padStart(2, "0"))
console.log(template, formatter(6)(numA), formatter(6)(numB))
console.log(template, formatter(0)(numA), formatter(0)(numB))
console.log(template, formatter(1)(numA), formatter(1)(numB))
/////////////////////////////////////////////
// console.log(template, formatter(2)(numA), formatter(2)(numB))
//// Precision is difference between JS and C++ languages
//// On future, the `options.roundingIncrement` and `options.roundingMode` parameters
//// will be resole that difference
console.log(template, "234.34", "45.70")
/////////////////////////////////////////////
console.log(template, formatter(3)(numA), formatter(3)(numB))
console.log(template,
formatter(6, { notation: "scientific" })(numA).replace(exponentialFormRegex, replacer).toLowerCase(),
formatter(6, { notation: "scientific" })(numB).replace(exponentialFormRegex, replacer).toLowerCase()
)
console.log(template,
formatter(6, { notation: "scientific" })(numA).replace(exponentialFormRegex, replacer).toUpperCase(),
formatter(6, { notation: "scientific" })(numB).replace(exponentialFormRegex, replacer).toUpperCase()
)
console.log(template, formatter(undefined)(numA), formatter(undefined)(numB))
console.log(template, formatter(undefined)(numA), formatter(undefined)(numB))
}
main()
Copy The Code &
Try With Live Editor
Output
#5 Code Example with Python Programming
Code -
Python Programming
from decimal import Decimal
i = 234.345
j = 45.698
print('%.6f - %.6f'%(i, j))
print('%.0f - %.0f'%(i, j))
print('%.1f - %.1f'%(i, j))
print('%.2f - %.2f'%(i, j))
print('%.3f - %.3f'%(i, j))
print('%.6e - %.6e' % (Decimal(i), Decimal(j)))
print('%.6E - %.6E' % (Decimal(i), Decimal(j)))
print('%g - %g'%(i, j))
print('%g - %g'%(i, j))
Copy The Code &
Try With Live Editor
Output