题目大意:鞍山赛区网络赛签到题,给出每首歌的分值,按照降序排列,每首歌按照0.95^(i-1)*ai来计算最后总共多少分。
解题思路:理解了题就拍了,以为会是签到题,结果一直WA,那个泪奔。分析了下可能是精度的问题,之前没涉及精度题,一直无语,后边发现可以换种思维解,避免精度丢失的,orz...,算是长见识了。思想就是倒过来求解,升序排列数组,然后每一步更新我的ans值,这样就能保证结果的中间计算不会丢失精度,这样我遍历一次最低值会计算i-1次,而最高位计算了一次。很巧妙的方法的,详见code。
题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=5003
code:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 50+5;
int t,n;
double ans;
int ai[MAXN];
int main(){
//freopen("input.txt","r",stdin);
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&ai[i]);
sort(ai,ai+n);
ans=0;
for(int i=0;i<n;i++)
ans=ans*0.95+ai[i];//避免精度丢失
printf("%.9f\n",ans);
}
return 0;
}