题目里面除了N和D为整数已知,其余的都不知道,不妨其他的都设为double。
小错误总结:这个题目里面,double的输入是%lf,输出是%f,第一遍直接都用了%f,导致了结果为0.00
#include<cstdio>
#include<algorithm>
using namespace std;
struct mooncake
{
double storage;
double price;
double bizhi;//按照比值由大到小排序
}mc[1010];
bool cmp(mooncake a, mooncake b)
{
return a.bizhi > b.bizhi;
}
int main()
{
int n,d;
scanf("%d %d", &n, &d);
for (int i = 0; i < n; i++)
{
scanf("%lf", &mc[i].storage);
}
for (int i = 0; i < n; i++)
{
scanf("%lf", &mc[i].price);
mc[i].bizhi = mc[i].price / mc[i].storage;
}
sort(mc, mc + n, cmp);
double profit=0.0;
for (int i = 0; i < n; i++)
{
if (mc[i].storage <= d)
{
profit += mc[i].price;
d -= mc[i].storage;
}
else
{
profit += mc[i].price * d/mc[i].storage;
break;
}
}
printf("%.2f", profit);
return 0;
}