分数化小数

题目:输入正整数a,b,c,输出a/b的小数形式,精确到小数点后c位。a,b <= 10^6,c <= 100。例如a=1,b=6,c=4时应输出0.1667.

分析:考察格式化输出。

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
    int a,b,c,x,y,mod,m,r,i;
    while(~scanf("%d %d %d",&a,&b,&c))
    {
        printf("%d",a/b);
        mod=a%b;
        if(c>0)
        {
            printf(".");
            for(i=1;i<c;i++)
            {
                m=mod*10;
                r=m/b;
                printf("%d",r);
                mod=m%b;
            }
            m=mod*10;
            x=m/b;
            mod=m%b;
            m=mod*10;
            y=m/b;
            if(y>=5)
               x++;
            printf("%d
",x);    
        }
    }
    return 0;
}

如果有更加好的方法请告诉我~

原文地址:https://www.cnblogs.com/lipching/p/3850985.html