I - Budget
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
题目大意:
n个小数点后面保留三位的数,现在要把这些三位数全都四舍五入变成两位数。问这些数变成两位全都变成两位数之后,与改变位数之前的差值是多少
思路:
直接用long double 来存的话,精度不高,测试极限的时候数字会乱
用字符串来存,因为是三位进二位,直接判断字符串最后一个字符能否四舍五入进二位。
代码如下:
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
char a[30];
char ti;
int i;
int u = 0;
for (i = 0; i < n; i++)
{
scanf("%s", a);
int len = strlen(a);
ti = a[len - 1];
if ((ti - '0') < 5)
{
u -= (ti - '0');
}
else
{
u += 10 - (ti - '0');
}
}
float shuchu = u / 1000.0;
printf("%.3lf\n", shuchu);
}
return 0;
}