题目
本题给我们直观的感受了————计算机底层对小数处理的精度的极限。
一、精度有问题的解法(未AC)
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,n;
while(~scanf("%d%d%d",&a,&b,&n))
{
char test[15]="%.";
char rear[4]="f\n";
char c[5];
sprintf(c,"%d",n);
strcat(test+1,c);
int len=strlen(test);
strcat(test+len,rear);
//printf函数的使用 ,下面这样输入2 3 3会显示0.667
printf(test,(double)a/(double)b);
}
return 0;
} 表现:
输入2 3 3会显示0.667,而不是题目中的0.666 用例: 1 3 788 对应输出应该为: 0.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333 你的输出为: 0.33333333333333331482961625624739099293947219848632812500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
二、改进后的AC代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,n;
while(~scanf("%d%d%d",&a,&b,&n))
{
printf("%d.",a/b);//整数
a%=b;
//精度到1000了,由于计算机底层实现,C语言无法弄那么高精度,只能用这种方式来模拟
while(n--)
{
a*=10;
printf("%d",a/b);
a%=b;
}
printf("\n");
}
return 0;
} 
京公网安备 11010502036488号