Budget

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)

Problem Description

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

思路:

如果直接输入小数的话等下的精度转换可能会出现问题,所以这里直接输入字符串,所以直接find找小数点的后三位就是最尾巴的数字了,因为是差额的总和,所以每一个差额算出来再相加就行了,而最后一位四舍五入然后得出的整除再除1000就是最后的答案了。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
using namespace std;
int main() {
    ios::sync_with_stdio;
    int n;
    string s;
    while (scanf("%d", &n) != EOF) {
        int sum = 0;
        for (int i = 0; i < n; i++) {
            cin >> s;
            int k = s.find('.');
            int temp = s[3 + k] - '0';
            if (temp <= 4) sum -= temp;
            else sum += 10 - temp;
        }
        double x = sum * 1.0 / 1000;
        printf("%.3lf\n", x);
    }
    return 0;
}