Decimal division

Title Description
TWQ classmate has done a lot of questions and feels that he is already very good. But today, one of the questions stumped him. The idea of ​​the question is very simple. It is to give you two numbers and calculate the result of dividing the two numbers. But The requirement is to keep to c digits after the decimal point. TWQ is a bit confused and does not know how to solve this problem, because the questions he did before were given the number of digits after the decimal point, but this question did not give the exact value. Please also be smart to help TWQ solve this problem.

Enter a
positive integer a, b, c (a, b<=100000, c<=1000), and enter the end of multiple test cases as a=b=c=0.

Output
the decimal form of a/b, accurate to c digits after the decimal point.

Sample input
5 9 3
0 0 0

Sample output
0.556

#include<stdio.h>
int main()
{
    
    
    int d[1000];
    int a, b, c, i, x;
    while(scanf("%d%d%d",&a,&b,&c),a!=0&&b!=0&&c!=0)
    {
    
    
        x=a%b;  
        d[0]=a/b;//计算小数点前面的数
        for(i=1 ;i<=c+1; i++)//计算c+1位小数
        {
    
    
            x=x*10;//余数扩大10倍进行运算
            d[i]=x/b;//商为要求的小数位
            x=x%b;//余数为下一次运算
            if(i==c+1&&d[c+1]>=5)
            {
    
    
                d[c]++;//判断末尾
            }
        }
        for(i=c; i>0; i--)
        {
    
    
            if(d[i]==10)//判断特殊情况,像0.99999这样的就要输出1.00000
            {
    
    
                d[i]=0;
                d[i-1]++;
            }
        }
        for(i=0; i<=c; i++)
        {
    
    
            if(i==0)
            printf("%d.",d[0]);
            else
            printf("%d",d[i]);
        }
        printf("\n");
    }
}

Guess you like

Origin blog.csdn.net/m0_46381590/article/details/111640371