PAT乙组 1020 月饼 (25)

题目链接: https://pintia.cn/problem-sets/994805260223102976/problems/994805301562163200
这道题使用结构体做会方便很多。另外这道题的一个坑点就是精度,我刚开始结构体里开的库存量和总售价是用int而平均价格用了double,结果在第三个测试点出错了。(23/25)第二次我就全部用double类型ac了。
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxn=1010;
const int INF=0x3f3f3f3f;
struct Node
{
	double v;
	double p;
	double av;
}s[maxn];
bool cmp(Node a,Node b)
{
	return a.av>b.av;
}
int main()
{
	int n;
	double d;
	double sum=0;
	cin>>n>>d;
	for(int i=0;i>s[i].v;
	for(int i=0;i>s[i].p;
		s[i].av=s[i].p*1.0/s[i].v;
	}
	sort(s,s+n,cmp);
	for(int i=0;i=s[i].v)
		{
			sum+=s[i].p;
			d=d-s[i].v;
		}
		else
		{
			sum+=d*s[i].av*1.0;
			break;
		}
	}
	printf("%.2f\n",sum);
}

你可能感兴趣的:(PAT乙组 1020 月饼 (25))