1070. Mooncake 解析

本文介绍了一种通过排序和逐个计算的方法来解决如何用最少的钱买到最多数量的某种商品(如月饼)的问题。该算法首先对每种月饼的单价进行排序,然后按顺序选择直到达到购买总量限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对单价排序 然后一个个算就好啦

#include <iostream>
#include <vector>
#include <algorithm>

#define MAX 1010

using namespace std;
int n, ton;

struct Node{
	float ton;
	float prise;
	float avg;
};
Node MoonCake[MAX];

bool cmp(Node n1, Node n2) {
	return n1.avg > n2.avg;
}

int main() {
	cin >> n >> ton;

	for(int i = 0; i < n; i++) {
		cin >> MoonCake[i].ton;
	}

	for (int i = 0; i < n; i++) {
		cin >> MoonCake[i].prise;
		MoonCake[i].avg =MoonCake[i].prise / MoonCake[i].ton;
	}

	sort(&MoonCake[0], &MoonCake[0] + n, cmp);

	int pos = 0;
	float profit = 0;
	while (ton && pos < n) {
		float left = ton - MoonCake[pos].ton;
		if (left < 0){
			profit += ton * MoonCake[pos].prise / MoonCake[pos].ton;
			break;
		}
		else {
			profit +=  MoonCake[pos].prise;
			ton = left;
			pos++;
		}
	}

	printf("%.2f\n", profit);

	return 0;


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值