为啥这题用float过不了啊?
查看原帖
为啥这题用float过不了啊?
129468
斧乃木余接楼主2021/12/2 23:26

如题,debug了很久也没找出错误在哪,代码的思路几乎和某个题解一模一样了。。。样例的数据规模又非常大,输不进去。最后把float全改成double就过了,一脸懵逼

#include<iostream> 
#include<cstdio>
#include<cmath>
#include<algorithm>

using namespace std;

int n;
double ans=0;
struct person{
	int t,n;
}people[1005];

bool cmp(person a,person b)
{
	return a.t<b.t;
}

int main() 
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>people[i].t;
		people[i].n=i;
		//cout<<endl<<n-i<<" "<<endl;
	} 
	//cout<<endl<<"gggggggggggggggggg"<<endl;
	sort(people+1,people+n+1,cmp);
	for(int i=1;i<=n;i++)
	{
		cout<<people[i].n<<" ";
		ans+=(double)(people[i].t*(n-i));
	}
	printf("\n%.2f\n",ans/n);
	return 0;
}
2021/12/2 23:26
加载中...