2-5 分數化小數()
阿新 • • 發佈:2018-11-14
輸入正整數a, b, c,輸出a/b的小數形式,精確到小數點後c位。a, b <= 10^6,c <= 100。輸入包含多組資料,結束標記為a=b=c=0.
樣例輸入:
1 6 4
0 0 0
樣例輸出:
Case 1:0.1667
分析:
嘗試利用另一種辦法分數化小數,先乘上10^c,留下 小數部分判斷是否四捨五入再除以10^c,但輸出結果有點問題。
#include <stdio.h> int main() { int a,b,c; double num,t,d; double p=10; int i,kase=1; while(scanf("%d %d %d",&a,&b,&c)!=0) { num=1.0*a/b; for(i=1;i<c;i++) { p*=10; } num*=p; t=(int)num/1; d=num-t; if(d>=0.5)t++; t/=p; if(kase)printf("\n"); printf("Case %d:%f",++kase,t); } return 0; } 附簡易程式碼 ``` #include <stdio.h> int main() { int a,b,c; double num; int kase=1; while(scanf("%d %d %d",&a,&b,&c)!=0) { num=1.0*a/b; if(kase)printf("\n"); printf("Case %d:%.*lf",++kase,c,num); } return 0; }