2020-06-03

Fractional decimals
Exercise 2-5 Fractional decimals (decimal) Input positive integers a, b, c, and output the decimal form of a/b, accurate to c places after the decimal point. a, b≤10⁶, c≤100. The input contains multiple sets of data, and the end tag is a=b=c=0.
Sample input: 1 6 4
0 0 0
Sample output: Case 1: 0.1667
Idea: I see printf("%.*f",(double)a/b) on the Internet, but the precision of double is only 16 digits.
First, use the loop to store the quotient of each bit in an array and then determine the carry situation (1. The c+1 bit <4 2. C+1 bit> 4 and c bit!=9 3. C+1 bit>4 And c bit == 9 In this case, we need to reverse the cycle to judge the previous bit. Finally, the integer and decimal values ​​that have been processed are stored in the array at this time. Just output a question to torment me It’s been a long time. I
Insert picture description herestarted doing this question last night. I thought about it for half an hour and didn’t think about it. I understood other people’s code for half an hour and mastered the basic algorithm. Then I copied other people’s code but couldn’t get the correct results. I looked for it for more than an hour. I realized that just because I lost an extra% in scanf, I didn’t report an error! I thought I knew this question, but whether I could write it out is another matter. It took another one and a half hours at noon, and it was deduction of details. I wrote the code on paper and thought it was okay. When I ran it on the computer at night, I found that there was a situation that could not run out, that is, the 0.9999 situation. Then I thought and meditated for two hours. Finally, I found that there is a debugging function. Using Baidu while using it, observing variables one by one, looking at the code execution step by step, finally succeeded in finding the bug. In the end, it took five and a half hours, and the moment I finally ran out, I was so excited that I couldn't speak.

Guess you like

Origin blog.csdn.net/brucecui1998/article/details/106533461