题意:给出数量算最大能卖多少钱
贪心就是一个每一步找最优解的思想
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std;
struct mooncake
{
double store;
double allcost;
double acost;
}cake[1001];
bool cmp(mooncake a, mooncake b)
{
return a.acost > b.acost;
}
int main()
{
int n;double m; double cost = 0;
cin >> n >> m;
for (int i = 0; i < n; i++)
{
cin >> cake[i].store;
}
for (int i = 0; i < n; i++)
{
cin >> cake[i].allcost;
cake[i].acost = cake[i].allcost / cake[i].store;
}
sort(cake, cake + n, cmp);
for (int i = 0; i < n; i++)
{
if (cake[i].store >= m)
{
cost += cake[i].acost * m;
break;
}
else
{
m -= cake[i].store;
cost += cake[i].allcost;
}
}
printf("%.2f", cost);
}