PAT 甲级 1070 Mooncake

#include 
using namespace std;

struct Mooncake{
	float inventory, sumValue, price; //  库存量 总价值 单价
	bool operator < (const Mooncake &m) const { return price > m.price; }
};

int main(){
	// freopen("1070.data", "r", stdin);
	int n, d; scanf("%d%d", &n, &d); // n个品种,d为需求量
	Mooncake mooncakes[n];
	for(int i = 0; i < n; i++)
		scanf("%f", &mooncakes[i].inventory);
	for(int i = 0; i < n; i++){
		scanf("%f", &mooncakes[i].sumValue);
		mooncakes[i].price = mooncakes[i].sumValue / mooncakes[i].inventory;
	}
	sort(mooncakes, mooncakes+n); // 按照单价从高到低进行排序
	float res = 0; int i = 0; // 计算最大profit
	while(d > 0){ 
		if(mooncakes[i].inventory <= d){
			res += mooncakes[i].sumValue;
			d -= mooncakes[i].inventory;
		}else{
			res += mooncakes[i].sumValue * d/mooncakes[i].inventory;
			d = 0;
			break;
		}
		if(++i == n)
			break;
	}
	printf("%.2f", res);
}

你可能感兴趣的:(PAT)