HDUOJ 6575 Budget
题目链接
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
比较容易想到的就是化成 i n t int int 扩大再减小来得到尾数,但是很明显数据很大,不能这么算(用 Java 大整数应该可以),所以换个思维,进不进位其实主要就看第三位,我们可以把数据当作字符串读入,直接判断最后一位即可,AC代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
char s[100];
int main(){
while(~scanf("%d",&n)){
double ans=0;
while(n--){
scanf("%s",s);
int len=strlen(s);
if(s[len-1]-'0'<=4) ans-=0.001*(s[len-1]-'0');
else ans+=0.001*(10-(s[len-1]-'0'));
}
printf("%.3f\n",ans);
}
}
更多推荐
HDUOJ 6575 Budget
发布评论