PAT 1070 Mooncake[一般]
1070 Mooncake (25)(25 分)
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
题目大意:给出月饼的需求量,并且给出每种月饼的总量及其总价格,要求按需求给出最大利润。
代码来自:https://www.liuchuo.net/archives/1912
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct mooncake{
float mount, price, unit;
};
int cmp(mooncake a, mooncake b) {
return a.unit > b.unit;
}
int main() {
int n, need;
cin >> n >> need;
vector<mooncake> a(n);//定义大小为n的向量
for (int i = ; i < n; i++)
cin >> a[i].mount;
for (int i = ; i < n; i++)
cin >> a[i].price;
for (int i = ; i < n; i++)
a[i].unit = a[i].price / a[i].mount;//计算单位价格
sort(a.begin(), a.end(), cmp);//按照单价进行排序。
float result = 0.0;
for (int i = ; i < n; i++) {
if (a[i].mount <= need) {
result = result + a[i].price;
} else {
result = result + a[i].unit * need;
break;
}
need = need - a[i].mount;
}
printf("%.2f",result);
return ;
}
//不是贪心问题啊,就是一个普通的题目,对单位进行排序。 不要被贪心吓到了啊。
PAT 1070 Mooncake[一般]的更多相关文章
- PAT 1070. Mooncake (25)
Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types ...
- PAT 1070 Mooncake
题目意思能搞成这样我也是服了这个女人了 #include <cstdio> #include <cstdlib> #include <vector> #includ ...
- PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...
- 1070 Mooncake (25 分)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn ...
- 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)
题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...
- PAT Advanced 1070 Mooncake (25) [贪⼼算法]
题目 Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many typ ...
- PAT (Advanced Level) 1070. Mooncake (25)
简单贪心.先买性价比高的. #include<cstdio> #include<cstring> #include<cmath> #include<vecto ...
- PAT甲题题解-1070. Mooncake (25)-排序,大水题
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...
- 1070. Mooncake (25)
题目如下: Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many ...
随机推荐
- 【swoole2.0】 PHP + swoole2.0 初体验
背景: centos7 PHP7.1 swoole2.0 准备工作: 一. swoole 扩展安装 1 下载swoole cd /usr/local wget -c https://git ...
- python框架---->APScheduler的使用
这里介绍一下python中关于定时器的一些使用,包括原生的sche包和第三方框架APScheduler的实现.流年未亡,夏日已尽.种花的人变成了看花的人,看花的人变成了葬花的人. python中的sc ...
- Ajax技术(WEB无刷新提交数据)
(转自:http://www.jb51.net/article/291.htm) Ajax内部交流文档一.使用Ajax的主要原因 1.通过适当的Ajax应用达到更好的用户体验: 2.把以前的一些服务器 ...
- android studio 引用远程仓库下载慢(JCenter下载慢)的办法
https://blog.csdn.net/linglingchenchen/article/details/62236723 解决android studio引用远程仓库下载慢(JCenter下载慢 ...
- Mysql 忘记 root密码解决
1 stop mysql Ubuntu/Debian: sudo /etc/init.d/mysql stop CentOs: sudo /etc/init.d/mysqld stop 2 启动saf ...
- fis前端开发框架
FIS是专为解决前端开发中自动化工具.性能优化.模块化框架.开发规范.代码部署.开发流程等问题的工具框架,相比gulp和grunt更傻瓜化,上手更容易,最近抽空学习了一下,分享一下心得. FIS官网: ...
- redis -clock_gettime问题
/home/wm/redis-/deps/jemalloc/src/nstime.c:: undefined reference to `clock_gettime' 这个错误 解决思路如下 .查找实 ...
- SSH教程从零打造在线网盘系统前言&目录
本系列教程内容提要 本系列教程是一个学习教程,是关于Java工程师的SSH(Struts2+Spring+Hibernate)系列教程,本教程将会分为四个部分和大家一同打造一个在线网盘系统,由于教程是 ...
- Asp.net MVC]Asp.net MVC5系列——Routing特性
目录 概述 路由特性 使用路由 可选参数和参数的默认值 路由前缀 默认路由 路由约束 自定义路由约束 路由名 区域(Area) 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列— ...
- ELk之使用kibana展示访问IP地图
参考文档:http://blog.51cto.com/ls40905250/1915280 https://blog.csdn.net/zsjwish/article/details/79792212 ...