这道题好狗啊,一看就会,一做就废。一开始总是出现精度丢失的问题,我以为是我的问题,题目写太少,是不是被人能用类似printf("%.2lf);之类的来让他不精度丢失,看着被人都过了,急了,用double精度丢失,用float好一点,前面的精度不会丢失,测试都是对的,交了几次都是WR,一度怀疑自己 ,直到看到了1e18,才感觉不是这么简单。
题目大意:将一组数据从保留小数点三维数字四舍五入到保留两位小数,输出结果和原来的数差了多少。
思路:用字符串去记录小数(就不会有精度的丢失),把最后的数a拿出来判断,大于等于5就相当于增加了(10-a),否则的话就是减少了a,差值就是每一组的a的和/1000
总结:精度丢失的话试试用字符串解决。
这是代码:
#include<iostream>
#include<stdlib.h>
#include<algorithm>
#include<queue>
#include<cstring>
#include<set>
#include<vector>
#include<stack>
#include<string>
//#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int T = 1;
void slove() {
int n;
while (~scanf("%d", &n)) {
double sum = 0;
for (int i = 0; i < n; i++) {
string a;
cin >> a;
ll len = a.size();
ll x = a[len-1] - '0';
if (x >= 5)x = 10 - x;
else x = -x;
sum += x;
}
printf("%.3lf\n", sum / 1000);
}
}
int main() {
//scanf("%d", &T);
while (T--) {
slove();
}
return 0;
}