PAT B1020
PAT B1020
解决思路 :贪心法,每次选取单价最高的月饼。
先上一个自己错误的解法
#include <cstdio>
#include <algorithm>
using namespace std;
double num[1010];
double price[1005];
double ans = 0;
int main() {
int n, d;
scanf("%d%d", &n, &d);
for (int i = 0; i < n; i++) {
scanf("%lf", &num[i]);
}
for (int i = 0; i < n; i++) {
scanf("%lf", &price[i]);
price[i] = price[i] / num[i];
}
sort(price, price + n);
for (int i = n - 1; i >= 0; i--) {
while (num[i] > 0 && d > 0) {
ans += price[i];
num[i]--;
d--;
}
}
printf("%.2f", ans);
return 0;
}
然后是题解
#include <cstdio>
#include <algorithm>
using namespace std;
struct mooncake {
double store; //库存
double sell; //总价
double price; /单价
}cake[1010];
bool cmp(mooncake a, mooncake b) {
return a.price > b.price;
}
int main() {
int n;
double D;
scanf("%d%lf", &n, &D);
for (int i = 0; i < n; i++) {
scanf("%lf", &cake[i].store);
}
for (int i = 0; i < n; i++) {
scanf("%lf", &cake[i].sell);
cake[i].price = cake[i].sell / cake[i].store;
}
sort(cake, cake + n, cmp);
double ans = 0;
for (int i = 0; i < n; i++) {
if (cake[i].store <= D) { //如果需求高于月饼的库存
D -= cake[i].store; //先把第i种全部卖出
ans += cake[i].sell;
} else { //库存高于需求
ans += cake[i].price * D; //只售出该种月饼,数量为需求量,然后break;
break;
}
}
printf("%.2f", ans);
return 0;
}
PAT B1020的更多相关文章
- 月饼问题PAT B1020(贪心算法)
月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意:销售时允许取出一部分库存.样 ...
- PAT B1020 月饼(25)
题目描述 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意:销售时允许取出一部 ...
- PAT B1020 月饼
#include<iostream> #include<algorithm> using namespace std; struct mooncake { double sto ...
- 【C/C++】贪心/算法笔记4.4/PAT B1020月饼/PAT B1023组内最小数
简单贪心 所谓简单贪心,就是每步都取最优的一种方法. 月饼问题:有N种月饼,市场最大需求量D,给出每种月饼的库存量和总售价. 思路:从贵的往便宜的卖.如果当前的已经卖完了,就卖下一个.如果剩余D不足, ...
- PAT题目AC汇总(待补全)
题目AC汇总 甲级AC PAT A1001 A+B Format (20 分) PAT A1002 A+B for Polynomials(25) PAT A1005 Spell It Right ( ...
- 《转载》PAT 习题
博客出处:http://blog.csdn.net/zhoufenqin/article/details/50497791 题目出处:https://www.patest.cn/contests/pa ...
- PAT Judge
原题连接:https://pta.patest.cn/pta/test/16/exam/4/question/677 题目如下: The ranklist of PAT is generated fr ...
- PAT/字符串处理习题集(二)
B1024. 科学计数法 (20) Description: 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+ ...
- PAT 1041. 考试座位号(15)
每个PAT考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座 ...
随机推荐
- 7、Dockerfile详解
参考: https://www.imooc.com/article/details/id/25229 https://www.cnblogs.com/panwenbin-logs/p/8007348. ...
- 小程序App.js 传递数据给实例(app异步数据问题)
在最开始初始化的时候,都会触发app.js 这个里面的onload生命方法, 在这个方法里面我们可以获取之前的存储数据/异步请求等等操作, 但是这些操作一般都是需要稍许时间.也就是说在其他界面加载结束 ...
- POJ 1061 青蛙的约会(拓展欧几里得)
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #i ...
- 异常:Error resolving template "xxx", template might not exist or might not be accessible...解决办法
在开发环境下正常,但使用jar运行时,报错Error resolving template template might not exist or might not be accessible,意思 ...
- oracle drop 表后 恢复
1.查看回收站中表 select object_name,original_name,partition_name,type,ts_name,createtime,droptime from recy ...
- 在html后面拼接字符串后页面的跳转
我就举一个简单的栗子,主要目的是实现页面跳转时后面获取的参数 <div class="active"> 点击我可以跳转 </div> 样式就随便写一下 之后 ...
- Android测试(三)——burpsuite抓包设置
导出证书: 将证书导入模拟器中: 设置监听端口,透明代理(一定要设置这个): 进入adb shell,输入如下命令,即可抓包了: iptables -t nat -A OUTPUT -p tcp - ...
- springmvc请求参数异常统一处理
1.ExceptionHandlerController package com.oy.controller; import java.text.MessageFormat; import org.s ...
- 【记录】【3】设置bing为chrome的默认搜索引擎
方法:设置→搜索→管理搜索引擎→其他搜索引擎→设置bing搜索的网址为 http://cn.bing.com/search?q=%s 注:search?q=%s 是必须的,否则无法将其设置为默认 ...
- javascript高级程序设计第3版——第6章 面向对象的程序设计
第六章——面向对象的程序设计 这一章主要讲述了:面向对象的语言由于没有类/接口情况下工作的几种模式以及面向对象语言的继承: 模式:工厂模式,构造函数模式,原型模式 继承:原型式继承,寄生式继承,以及寄 ...