【PTA刷题整理】PAT 乙级 1020 月饼

2020.03.09 不知道为什么做完今天这个题感到非常的沮丧


1020 月饼 (25分)

月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。
注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有 3 种月饼,其库存量分别为 18、15、10 万吨,总售价分别为 75、72、45 亿元。如果市场的最大需求量只有 20 万吨,那么我们最大收益策略应该是卖出全部 15 万吨第 2 种月饼、以及 5 万吨第 3 种月饼,获得 72 + 45/2 = 94.5(亿元)。
输入格式:
每个输入包含一个测试用例。每个测试用例先给出一个不超过 1000 的正整数 N 表示月饼的种类数、以及不超过 500(以万吨为单位)的正整数 D 表示市场最大需求量。随后一行给出 N 个正数表示每种月饼的库存量(以万吨为单位);最后一行给出 N 个正数表示每种月饼的总售价(以亿元为单位)。数字间以空格分隔。
输出格式:
对每组测试用例,在一行中输出最大收益,以亿元为单位并精确到小数点后 2 位。
输入样例:

3 20
18 15
10 75
72 45

输出样例:

94.50


今天这个题是我写的最郁闷的一次,这道题以后还要回来重做,数组的SIZE开太大在Dev里面会运行不了 , 数组开的小会出现PAT里面的第五个测试点段错误(多开一位就没了???)
首先求出每个月饼的单价,同时计算一个有多少份单价,然后对单价从大到小进行排序(可以使用sort()函数结合greater(数据类型)),分成每份从0开始向上累加,直到逼近D但小于D为止
值得注意的是一个地方,只说了N是正整数,其他都应该定义为double(除了计数器),不然的话第二组测试点会出错(就这个搞了我二十多分钟)

#include<iostream>                  //输入输出流头文件
#include<iomanip>
#include<stdio.h>                   //标准输入输出
#include<stdlib.h>
#include<math.h>                    //数学函数
#include<string.h>                  //C语言字符数组的字符串
#include<algorithm>                 //C++标准模板库的函数
#include<map>                       //map映射容器
#include<unordered_map>             //无序的map映射容器
#include<vector>                    //变长数组容器
#include<queue>                     //队列
#include<stack>                     //栈
#include<string>                    //C++string类
#include<set>                       //set集合
#define SIZE 1000000
using namespace std;                //标准命名空间

                                    //可以加入全局变量或者其他函数

struct MoonCake{
	double Category;
	double Price;
}; 

int main(){                         //主函数
#ifdef ONLINE_JUDGE                 //如果有oj系统(在线判定),则忽略文件读入,否则使用文件作为标准输入
#else
    freopen("1.txt", "r", stdin);   //从1.txt输入数据
#endif
	int N;
	double D , ans = 0;
	cin >> N >> D;
	int price , counter = 0;
	vector<MoonCake> temp;
	double list[SIZE];
	for(int i = 0 ; i < N ; i++){
		MoonCake a ;
		cin >> a.Category;
		temp.push_back(a);
	}
	for(int j = 0 ; j < N ; j++){
		cin >> price;
		temp[j].Price = price;
	}
	for (int i = 0; i < N; i++){
		for (int j = 0; j < temp[i].Category; j++){
			list[counter++] = (double)temp[i].Price / temp[i].Category;
		}
	}
	sort(list , list + counter , greater<double>());
	for(int k = 0 ; k < D ; k++){
		ans += list[k];
	}
	printf("%0.2lf\n" , ans);
    return 0;                       
}

在这里插入图片描述

网上看到的正解

这个vector结合pair的优化真的是看的我自愧不如,段位还是太低了害,这里附上大佬的原链接,有很详细的分析
PAT1020的三种解法

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
	int n, id = 0;
	double D, sum = 0, tmp;
	cin >> n >> D;
	vector<pair<double, double>> mc(n);
	for (int i = 0; i < n; ++i)
		cin >> mc[i].first;
	for (int i = 0; i < n; ++i) {
		cin >> tmp;
		mc[i].second = tmp / mc[i].first;
	}
	sort(mc.begin(), mc.end(),
		[](const auto& a, const auto& b) {
			return a.second > b.second;
		});
	while (id<n&&mc[id].first < D) {
		sum += mc[id].second * mc[id].first;
		D -= mc[id++].first;
	}
	if (id < n && D)
		sum += D * mc[id].second;
	printf("%0.2lf",sum);
 	return 0;
}

发布了22 篇原创文章 · 获赞 2 · 访问量 494

猜你喜欢

转载自blog.csdn.net/weixin_43849089/article/details/104758836
今日推荐