PAT Basic Level 1020 月饼 解题思路及AC代码

PAT B1020 月饼+测试点3答案错误 1020 月饼 (25 分) 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。 注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有 3 种月饼,其库存量分别为 18、15、10 万吨,总售价分别为 75、72、45 亿元。如果市场的最大需求量只有 20 万吨,那么... 阅读详情

1. 题目简述及在线测试位置

1.1 已知各种月饼的库存量及其总价 和 市场需求量,求最大收益
1.2 在线测试位置:PAT 1020 月饼

2. 基本思路

2.1 最大收益意味着价位高的月饼优先卖出,将月饼按平均价格从高到低排序,依次取出,直到所有月饼取出 或 取出的月饼总量达到市场需求量
2.2 有两个测试点需要注意:市场需求量可能大于月饼库存总量、月饼的库存可能是小数

解题用的数据结构是结构体数组

#define MAX 1001
struct MoonCake
{
	float Quantity; //月饼数量
	float Price; //月饼总价
	float Average; //月饼均价(月饼总价/月饼数量)
};

3. AC代码

#include <algorithm>
#include <iostream>
using namespace std;

#define MAX 1001

struct MoonCake
{
	float Quantity; //月饼数量
	float Price; //月饼总价
	float Average; //月饼均价(月饼总价/月饼数量)
};

//按月饼的平均价格从大向小排序
bool Compare(struct MoonCake A, struct MoonCake B)
{
	return A.Average > B.Average;
}

int main()
{
	int Kind, SumQuantity; //月饼种类、市场需要总量
	float quantity, price; //月饼单品数量、月饼单品总价
	float CurrentSumQuantity=0,SumEarning=0;

	scanf("%d %d", &Kind, &SumQuantity);

	struct MoonCake MCake[MAX]; //初始化均价
	for (int i = 0; i < MAX; i++)
		MCake[i].Average = 0;

	for (int i = 0; i < Kind; i++)
	{
		scanf("%f", &quantity);
		MCake[i].Quantity = quantity;
	}

	for (int i = 0; i < Kind; i++)
	{
		scanf("%f", &price);
		MCake[i].Price = price;
		MCake[i].Average = MCake[i].Price / MCake[i].Quantity;
	}

	sort(MCake, MCake + Kind,Compare); //按月饼的平均价格从大向小排序,后面按此顺序取出月饼
	
	for (int i = 0; i < Kind; i++) //循环正常结束:库存总量小于市场需求总量;执行break:库存>=市场
	{
		if (MCake[i].Quantity + CurrentSumQuantity >= SumQuantity)
		{
			SumEarning += float(SumQuantity - CurrentSumQuantity) * MCake[i].Average;
			break;
		}
		else
		{
			CurrentSumQuantity += MCake[i].Quantity;
			SumEarning += MCake[i].Price;
		}
	}

	printf("%0.2f", SumEarning); 
	
	return 0;
}
PTA 六度空间 思路分析及代码解析 图的广度优先搜索的应用,通过解决实际问题,进一步熟悉图这种抽象数据结构 阅读详情

相关推荐

PTA 旅游规划(邻接矩阵) 思路分析及代码解析

单源有权无向图最短路问题,使用Dijkstra算法进行求解

thexiaosi的博客 2477

PTA L2-003 月饼 (25 分)

月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。 注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有 3 种月饼,其库存量分别为 18、15、10 万吨,总售价分别为 75、72、45 亿元。如果市场的最大需求量只有 20 万吨,那么我们最大收益策略应该是卖出全部...

好学者的博客 2260

PTA 电话聊天狂人 思路分析及代码解析

Hash表编码实践(链表形式)

thexiaosi的博客 2053

PAT1020 月饼 (25 分)

测试点3的问题 在于剩余的需求还存在,但是库存没有了,我的前面的判断出循环的条件是需求没了并且N不是0, 修改在于 当前种类的月饼库存小于剩余需求,并且此种类是排序后的嘴一个元素 #include<stdio.h> struct product{ double save; double sumprice; double per;//单价用来最为选择的标准 }; void sor...

AXKDD6555的博客 300

PTA QQ帐户的申请与登陆 思路分析及代码解析

通过Hash表(开放地址)存储和查找字符串数据

thexiaosi的博客 2009

PTA 关键活动 思路分析及代码解析

在实践中进一步理解拓扑排序,AOE,关键活动

thexiaosi的博客 1916

pat 乙级 1020 月饼 (25 point(s))

题意:样例第一个是月饼的种类,第二个是月饼的需求,后面一行是这几种月饼的总库存,而后一行是这几种月饼的总售价。 思路:把每种月饼的单价求出来,按从大到小的顺序排列(先卖值钱的),如果该月饼的库存比需求大,那么收益就是该月饼的总销量(不要拿单价*需求,单价是算出来的,存在误差),如果是小于库存的话,第一个全部卖完,需求减去第一个的库存,然后卖第二个,以此类推。 代码: #include<iostream> #include<vector> #include <algo

m0_51711089的博客 2607

PTA 一元多项式的乘法与加法运算 思路分析及代码解析 v0.93

锻炼链表的使用:创建和插入

thexiaosi的博客 1873

1020 月饼 (25分)测试点三,答案正确通不过

我笑了呀今天,我踩了两天的坑,昨天就因为一个+=1和++卡了一下午,今天虽然写错了代码有错在先,但是他妈的这个题把第二批货拉了刚好一半,我刚好算的是另外一半,答案都对就是过不去,回头看的时候也没想到是那个公式的问题,我fo啦!!!! 解决的方法是看柳神代码之后无意间发现的我的漏洞, ...

weixin_43723581的博客 670

手把手教你用Cadence Virtuoso完成LNA全套仿真:基于SpectreRF手册的实战补充

本文详细指导如何使用Cadence Virtuoso和SpectreRF完成LNA的全套仿真,涵盖噪声仿真、增益压缩、THD分析及双音IP3测量等关键步骤。针对官方手册中的常见问题,提供了12个实战解决方案,包括hbnoise函数选择、双音信号源配置等,帮助工程师高效完成射频IC设计。

weixin_29102619的博客 537

C# 【WebService编写调用】

😹 觉得博主文章写的不错的话,希望大家三连(✌关注,✌点赞,✌评论),多多支持一下!!!

只有登上山顶,才能看到那边的风光 1179

PAT(Basic level)Practice 1020 月饼 题解

1020 月饼 (25 分) 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。 注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有 3 种月饼,其库存量分别为 18、15、10 万吨,总售价分别为 75、72、45 亿元。如果市场的最大需求量只有 20 万吨,那么我们...

masteren的博客 606

实战指南 | IC模拟版图设计从零到精通,手把手带你掌握EDA工具与GDSII输出

本文提供了一份从零开始的IC模拟版图设计实战指南。详细介绍了如何搭建Linux环境、安装Cadence Virtuoso等EDA工具,并深入解析了PDK设计规则。通过手把手绘制CMOS反相器版图的实例,系统讲解了版图绘制、DRC/LVS验证、寄生参数提取、匹配设计以及ESD防护等核心技能,最终指导如何正确生成交付芯片制造厂的标准GDSII文件,帮助读者掌握完整的模拟版图设计流程。

weixin_29214691的博客 404

【PAT乙级】PAT (Basic Level) Practice (中文)训练记录

本博客用于记录PAT乙级题库做题新得

初见还是重逢的博客 4687

使用Intel编译器编译OpenFOAM

本文总结了使用INTEL编译器编译OpenFOAM的过程

姜蜉蝣的博客 1560

金融波动率的“捕手”:GARCH模型,揭秘市场波动的“聚集密码”

摘要: GARCH模型(广义自回归条件异方差)是金融时间序列分析中用于捕捉“波动聚类”现象的核心工具。传统模型(如ARIMA)无法刻画金融数据的时变波动特性,而GARCH通过建模条件方差的动态变化(如$\sigma_t^2 = \omega + \alpha \varepsilon_{t-1}^2 + \beta \sigma_{t-1}^2$),精准预测波动率。Python实战显示,GARCH能有效拟合模拟金融数据的波动聚类,残差无自相关,验证了其优越性。该模型在波动率预测、风险价值(VaR)计算和期权定

u010257409的博客 2081

Pat(Basic Level)Practice--1020(月饼)

Pat1020代码 解题思路:贪心算法,没什么好说的。。。#include #include typedef struct MCake { float left; float price; float value; }MCake; int comp(const void *a,const void *b) { return (*(MCake *)a).value<(*(MCake *

奋起的Coder。。。 1435

FCTA/FCTB 前方交通穿行预警/制动系统技术规范

FCTA&FCTB前方交叉路口预警制动系统是针对十字路口和停车场等"鬼探头"场景设计的驾驶辅助系统。系统在自车速度0-40km/h范围内工作,通过角雷达和摄像头检测侧向来车.

未知艺术家的博客 761

PAT (Basic Level) 1020 月饼

题意 给出各种月饼的库存和总价,问给定总量下的月饼最大收益? 思路 按性价比贪心呗。结构体排序题,注意比较函数用乘法代替除法。 代码 #include <bits/stdc++.h> using namespace std; struct cake { double num, val; }; int main() { ios::sync_with_stdio(false); ci...

abcdefbrhdb的博客 179

五十天精通硬件设计第6天-放大器知识及选型

运算放大器(Operational Amplifier,简称运放)是一种高增益的直流耦合差分放大器,广泛应用于信号处理、滤波、比较、积分、微分等电路中。

whm128的博客 604

【ComfyUI】Wan2.2 Rapid-AIO-Mega 二次元文生视频

通过 SD3 采样体系与 Wan 系列专用的 **WanVaceToVideo** 视频生成节点,整个流程从模型加载、文案编码、潜空间采样到视频合成一气呵成,能够稳定输出画面精致、角色统一、光影氛围鲜明的动态画面。这套工作流重点整合了视频长度、帧率、分辨率等核心参数控制,并结合 KSampler 的细节生成能力,旨在让用户能在 ComfyUI 中快速生成高质量的二次元叙事视频。

Mr数据杨 2015

PAT Basic Level 1020 月饼(C++实现)

月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。 注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有 3 种月饼,其库存量分别为 18、15、10 万吨,总售价分别为 75、72、45 亿元。如果市场的最大需求量只有 20 万吨,那么我们最大收益策略应该是卖出全部 15 万吨第 2 种月饼、以及 5 万吨第 3 种月饼,获得 72 + 45/2 = 94.5(亿元)。 输入格式

菜鸟 325

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

PAT上面一道关于“月饼”的题目的解法

题目详细描述见PAT:月饼 (25)。根据题意,首先要得到每种月饼的价格,根据价格的大小选择要销售的月饼,最后计算最大收益。具体到编程,有多种方法。        思路1:计算得到每种月饼的价格,然后将每种月饼的价格和每种月饼的总售价存放在一个pair类型的vector容器中,再利用STL中sort()函数对vector的元素进行排序,可得到最终结果。代码如下: #include #inclu

竭尽全力的专栏 2447

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain&#39;s apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath(&#39;DeepLearnToolbox&#39;));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand(&#39;state&#39;,0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W&#39;); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand(&#39;state&#39;,0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = &#39;sigm&#39;;%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, &#39;Too big error&#39;);```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand(&#39;state&#39;,0)sae = saesetup([784 100]);sae.ae{1}.activation_function = &#39;sigm&#39;;sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)&#39;)% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;;nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, &#39;Too big error&#39;);```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x&#39;,28,28,60000))/255;test_x = double(reshape(test_x&#39;,28,28,10000))/255;train_y = double(train_y&#39;);test_y = double(test_y&#39;);%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you&#39;ll get around 1.2% errorrand(&#39;state&#39;,0)cnn.layers = { struct(&#39;type&#39;, &#39;i&#39;) %input layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 6, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %sub sampling layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 12, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, &#39;Too big error&#39;);```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, &#39;Too big error&#39;);%% ex2 neural net with L2 weight decayrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex3 neural net with dropoutrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex4 neural net with sigmoid activation functionrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex5 plotting functionalityrand(&#39;state&#39;,0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = &#39;softmax&#39;; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand(&#39;state&#39;,0)nn = nnsetup([784 20 10]); nn.output = &#39;softmax&#39;; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

PTA 公路村村通 思路分析及代码解析

通过Prim算法解决最小生成树问题

thexiaosi的博客 3817

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

进阶练习 树相关问题(未解决)

树相关练习题,对逻辑思维能力要求很高,内文是大神们的一些思路(目前还未解决)

thexiaosi的博客 3683

PTA 排序 思路分析及代码解析

介绍了 冒泡排序、插入排序、选择排序、希尔排序、合并排序、堆排序基本的实现思路

thexiaosi的博客 2293
上一篇: PAT Basic Level 1019 数字黑洞 解题思路及AC代码
下一篇: PAT Basic Level 1021 个位数统计 解题思路及AC代码
thexiaosi
博客等级 码龄11年 62粉丝 169原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值