PAT 1070. Mooncake (25)

本文介绍了一个关于中秋节传统食品——月饼的销售策略问题。通过给定不同种类月饼的库存数量及单价,结合市场最大需求量,利用贪心算法解决如何最大化总利润的问题。

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

Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types of fillings and crusts can be found in traditional mooncakes according to the region's culture. Now given the inventory amounts and the prices of all kinds of the mooncakes, together with the maximum total demand of the market, you are supposed to tell the maximum profit that can be made.

Note: partial inventory storage can be taken. The sample shows the following situation: given three kinds of mooncakes with inventory amounts being 180, 150, and 100 thousand tons, and the prices being 7.5, 7.2, and 4.5 billion yuans. If the market demand can be at most 200 thousand tons, the best we can do is to sell 150 thousand tons of the second kind of mooncake, and 50 thousand tons of the third kind. Hence the total profit is 7.2 + 4.5/2 = 9.45 (billion yuans).

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N (<=1000), the number of different kinds of mooncakes, and D (<=500 thousand tons), the maximum total demand of the market. Then the second line gives the positive inventory amounts (in thousand tons), and the third line gives the positive prices (in billion yuans) of N kinds of mooncakes. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the maximum profit (in billion yuans) in one line, accurate up to 2 decimal places.

Sample Input:
3 200
180 150 100
7.5 7.2 4.5
Sample Output:
9.45

思路:实质上是部分背包问题,每次贪心的取最大单位价值的月饼 

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

using namespace std;

class Mooncake {
public:
	double w, p;
};

bool comp(const Mooncake &m1, const Mooncake &m2) //单位价值高的在前面
{
	return (m1.p*m2.w > m2.p*m1.w);
}

int main()
{
	int n, m;
	cin >> n >> m;
	vector<Mooncake> vm(n);
	for (int i=0; i!=n; ++i)
		cin >> vm[i].w;
	for (int i=0; i!=n; ++i)
		cin >> vm[i].p;
	sort (vm.begin(), vm.end(), comp);
	double sum = 0.0;
	int i = 0;
	while (m && i<n) { //当全部量采集够或者把能买的都买了则停止
		if (m >= vm[i].w) {  
			sum += vm[i].p;
			m -= vm[i].w;
		} else {  //只需要部分量
			sum += (double)m*vm[i].p/vm[i].w;
			m = 0;
		} 
		++i;
	}
	cout << setiosflags(ios::fixed) << setprecision(2) << sum << endl; 
  return 0;
}


对于PAT (Programming Ability Test) 编程能力考试中的题目1020 月饼,此题目的目标是在给定各种类月饼的库存数量、每种月饼单位重量的价值以及市场能够接受的最大总量的情况下,求解出可以得到的最大收益。 关于第三个测试点的具体情况没有直接提及,但通常这类问题会涉及到贪心算法的应用。解决此类问题的一般策略如下: 确保输入数据正确读取并解析。 ```c++ #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Mooncake { double stock; double pricePerUnitWeight; }; bool compare(const Mooncake& a, const Mooncake& b){ return a.pricePerUnitWeight > b.pricePerUnitWeight; } int main() { int n; // 种类数 cin >> n; vector<Mooncake> mooncakes(n); for(int i = 0; i < n; ++i) cin >> mooncakes[i].stock; for(int i = 0; i < n; ++i){ double totalValue; cin >> totalValue; if(mooncakes[i].stock != 0) mooncakes[i].pricePerUnitWeight = totalValue / mooncakes[i].stock; else mooncakes[i].pricePerUnitWeight = 0; } double demand; cin >> demand; sort(mooncakes.begin(), mooncakes.end(), compare); double maxProfit = 0.0; for(auto &m : mooncakes) { if(demand <= 0 || m.stock == 0) break; if(m.stock <= demand) { maxProfit += m.stock * m.pricePerUnitWeight; demand -= m.stock; } else { maxProfit += demand * m.pricePerUnitWeight; demand = 0; } } cout << fixed << setprecision(2) << maxProfit << endl; } ``` 针对第三个测试点可能遇到的问题及解决方案包括但不限于以下几点: 处理精度误差:由于浮点运算可能存在舍入误差,在比较两个浮点数值是否相等或者输出最终结果时需要特别小心。可以通过设置一个很小的阈值来判断两数是否足够接近以视为相等,同时在输出时使用`setprecision()`函数控制小数位数。 考虑边界条件:例如当某种类型的月饼库存为零但是仍然给出了总价的情况;或者是市场需求量小于等于零的时候应该如何返回正确的答案。 检查排序逻辑:保证按照单价从高到低排列所有种类的月饼,以便优先卖出价值更高的商品获取最大利润。 验证输入输出格式:确认程序能正确地接收输入参数并且按照规定的方式打印输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值