【来源】
【分析】
为了获取最大的利润,首先把月饼按照单价从高到低排序,然后依次购买,到满足需求为止。
【源码】
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
struct mooncake
{
double stock;
double money;
};
bool compare(mooncake lhs, mooncake rhs)
{
return lhs.money / lhs.stock < rhs.money / rhs.stock;
}
int main()
{
int n, d;
cin >> n >> d;
vector<mooncake> cakes;
for (int i = 0; i < n; ++i){
mooncake mk;
cin >> mk.stock;
cakes.push_back(mk);
}