Original Question
Test instructions: Input positive integer a,b,c, output A/b decimal form, accurate to C bit after decimal point. Among them a,b≤106, c≤100. The input contains multiple sets of data and the end tag is a=b=c=0.
Test data:
Sample input:
1 6 4
0 0 0
Sample output:
Case 1:0.1667 Problem solving thinking
This problem is integer rounding, take the remainder of the exercises.
This thought could be solved directly with C + + setprecision (c), but the test found that once C is greater than 16, the output of the floating-point number is followed by a total of 0, because the effective precision of double is only 16 bits.
So it is necessary to manually simulate the operation of division, with the elementary school when the study of the non-stop "multiply 10" method can be solved. First output the number before the decimal point, because the last one in the test sample needs to be rounded, so the first c-1 bit is output, and the last C bit is rounded based on the c+1 bit. AC Code
#include <iostream>
#include <cstdio>
using namespace std;
int main (int argc, char const *argv[])
{
int a,b,c,kase = 0;
while (~SCANF ("%d%d%d", &a, &b, &c))
{
if (!a &&!b &&!c) break;
kase++;
First output the digits before the decimal point
printf ("Case:%d:%d.", Kase, A/b);
a%= b;
C-1 bits after the decimal point for
(int i = 0; i < c-1; ++i)
{
a *=
; printf ("%d", A/b);
a%= b; A is the remainder of a c-1 bitwise operation when jumping out
}
//Consider the last one rounded
int more = ((a*10)%b *)/b;//Observe the next bit of the C-bit
if (more >= 5)
p rintf ("%d\n", (a*10)/b + 1);
else printf ("%d\n", (a*10)/b);
}
return 0;
}
The test is as follows: