Avin’s company has many ongoing projects with different budgets. His company records the budgets using numbers rounded to 3 digits after the decimal place. However, the company is updating the system and all budgets will be rounded to 2 digits after the decimal place. For example, 1.004 will be rounded down
to 1.00 while 1.995 will be rounded up to 2.00. Avin wants to know the difference of the total budget caused by the update.
Input
The first line contains an integer n (1 ≤ n ≤ 1, 000). The second line contains n decimals, and the i-th decimal ai (0 ≤ ai ≤ 1e18) represents the budget of the i -th project. All decimals are rounded to 3 digits.
Output
Print the difference rounded to 3 digits..
Sample Input
1 1.001 1 0.999 2 1.001 0.999
Sample Output
-0.001 0.001 0.000
题目大意:给出若干个三位小数,要求将每个三位小数四舍五入到两位小数后的差值相加给出。
题目思路:由于输入的数字范围过大且精度较高,不管是用float类型还是double类型都无法解决,故用字符串输入。由于四舍五入只与第三位小数有关且每个输入都是三位小数,所以只需要找到最后一个数并判断是否小于5。
代码如下:
#include<stdio.h>
#include<string.h>
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
char c[10000];
double num=0;
int i;
int a1;
for(i=0;i<n;i++)
{
scanf("%s",c);
int leng;
leng=strlen(c);
a1=c[leng-1]-'0';
if(a1>=5)
num+=(10-a1%10);
else
num-=(a1%10);
}
printf("%.3f\n",num/1000);
}
return 0;
}