ORIGIN

Budget HDU-6575

ACM 2 mins357 words

Budget

Problem

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
2
3
4
5
6
1
1.001
1
0.999
2
1.001 0.999

Sample Output

1
2
3
-0.001
0.001
0.000

Analysis

This is a rounding problem. Since it has only three number after the point, you just need to find the last number $n$ and if it’s more than four, the company over calculate it’s money by $10-n$ , if it’s equal or less than four, the company miss calculate the money by $n$. So the total difference is the over calculated money - the miss calculated money.

Note: you better not to calculating the floating number many times because there might be a precision problem. At first I time all the number to 1e2, let the computer automatically do the rounding for me. However there is always a precision problem and I don’t know how to fix. So I changed the way.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<bits/stdc++.h>

using namespace std;
int main() {
int n;
cin >> n;
int sum = 0;
for(int i = 0; i < n;i ++){
string a;
cin >> a;
int l = a.length();
if(a[l - 1] - '0' < 5 ) // miss calculated
sum -= a[l - 1] - '0';
else sum += (10 - (a[l - 1] - '0')); // over calculated
}
printf("%.3f\n", sum * 1.0 / 1000);
return 0;
}
TOP
COMMENT
  • ABOUT
  • |
o_oyao
  The Jigsaw puzzle is incomplete with even one missing piece. And I want to be the last piece to make the puzzle complete.
Like my post?
Default QR Code
made with ❤️ by o_oyao
©o_oyao 2019-2024

|