Everyone knows that a simple calculator can only compute up to about thirty decimal places.
But the curious Sangye wants to know more precise decimal values.
Please help poor Sangye by creating a program that can perform precise decimal calculations.
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
EOF input: Each input consists of three positive integers a, b, and N. 1<= a , b <= 2147483647 1 <= N <= 10000 (The input will not exceed 1000 lines) | Please output the result of the decimal operation a/b. Precise to N decimal places. Please round down after the Nth decimal place. |
18467 41 10 26500 6334 10 15724 19169 10 10 5 3 | 450.4146341463 4.1837701294 0.8202827481 2.000 |
Thought Process
In this problem, if using cin, optimization is needed to avoid timeouts. Since printf cannot be used to specify the number of decimal places, after outputting the integer part of the answer, run a loop N times to calculate the value after the decimal point for a/b. Take the remainder of a/b, then multiply a by 10 while outputting a/b.
Sample Code-ZeroJudge A248: Training ~ Array
#include <iostream>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(nullptr);
int a, b, N;
while (cin >> a >> b >> N)
{
cout << a/b << ".";
for (int i = 0; i<N; i++)
{
a %= b;
a *= 10;
cout << a/b;
}
cout << "\n";
}
}
//ZeroJudge A248
//Dr. SeanXD