贪心--FatMouse' Trade(结构…
2017-02-18 09:33
134 查看
FatMouse prepared M pounds of cat food, ready to trade with the
cats guarding the warehouse containing his favorite food,
JavaBean.The warehouse has N rooms. The i-th room contains Jii
pounds of JavaBeans and requires Fii
pounds of cat food. FatMouse does not have to trade for all the
JavaBeans in the room, instead, he may get Jii
* a% pounds of JavaBeans if he pays Fii
* a% pounds of cat food. Here a is a real number. Now he is
assigning this homework to you: tell him the maximum amount of
JavaBeans he can obtain.
Input
The input consists of multiple test cases. Each test case
begins with a line containing two non-negative integers M and N.
Then N lines follow, each contains two non-negative integers
Jii
and Fii
respectively. The last test case is followed by two -1's. All
integers are not greater than 1000.Output
For each test case, print in a single line a real number
accurate up to 3 decimal places, which is the maximum amount of
JavaBeans that FatMouse can obtain.Sample Input
Sample Output
cats guarding the warehouse containing his favorite food,
JavaBean.The warehouse has N rooms. The i-th room contains Jii
pounds of JavaBeans and requires Fii
pounds of cat food. FatMouse does not have to trade for all the
JavaBeans in the room, instead, he may get Jii
* a% pounds of JavaBeans if he pays Fii
* a% pounds of cat food. Here a is a real number. Now he is
assigning this homework to you: tell him the maximum amount of
JavaBeans he can obtain.
Input
The input consists of multiple test cases. Each test case
begins with a line containing two non-negative integers M and N.
Then N lines follow, each contains two non-negative integers
Jii
and Fii
respectively. The last test case is followed by two -1's. All
integers are not greater than 1000.Output
For each test case, print in a single line a real number
accurate up to 3 decimal places, which is the maximum amount of
JavaBeans that FatMouse can obtain.Sample Input
5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1
Sample Output
13.333 31.500
一只老鼠有M磅的猫粮,另外有一只猫控制了老鼠的N个房间,这些房间里面放了老鼠爱吃的绿豆,给出每个房间的绿豆数量,和这个房间的绿豆所需要的猫粮数,现在要求老鼠用这M磅的猫粮最多能换到多少它爱吃的绿豆?
贪心题,由于所有的绿豆都是一样的,所以如果老鼠想要换到最多的绿豆,便可以换猫控制的房间里面最便宜的绿豆,也就是说先换取单位数量的绿豆所需要最少的猫粮的房间里的绿豆,这样就可以保证换到的绿豆是最多的。具体实现可以用一个结构体,里面保存每个房间里面有的绿豆的数量和换取这个房间的绿豆时所需要的猫粮的数量和换取这个房间的 单位重量的绿豆所需要的猫粮数(以下简称单价),然后再按照单价升序给这些结构体排一次序,这时就可以从最便宜的绿豆开始换了。#include#include#includeusing namespace std;struct Room //struct必须在cmp前{ int J; int F; double JF;}room[10005]; //3个数相关联bool cmp(const Room a, const Room b){ return a.JF > b.JF; //>为升序,<为降序}int main(){ int M, N; while (cin >> M >> N&&(N!=-1)&&(M!=-1)) { for (int i = 0; i < N;i++) { cin >> room[i].J >> room[i].F; room[i].JF = (double)room[i].J / room[i].F; } sort(room, room + N, cmp); //结构体排序(若数列从1开始,则需在起末位置各+1 double ans=0; for (int i = 0; i < N; i++) //挨个房间换豆子直到不够换某一个房间所有豆子,按比例换并退出 { if ((M - room[i].F) >= 0) { ans += room[i].J; M = M - room[i].F; } else { ans += M*room[i].JF; break; } } printf("%.3lf\n", ans); } return 0;}
相关文章推荐
- 11.2.2 写结构相等的测试
- zoj 2109 || hdoj1009 FatMouse' Trade(贪心)
- zoj 2109 FatMouse' Trade简单的贪心 (注意double数组的排序问题)
- Nand&nbsp;Flash存储结构及控制方法(K9F…
- ZJU2109 FatMouse' Trade - 简单贪心
- JAVA 树形结构程序事件实例
- java&nbsp;swing&nbsp;树结构代码
- HDOJ 1009 FatMouse' Trade简单的贪心
- HDU1009 FatMouse' Trade(经典贪心)
- HDU 1009 FatMouse' Trade(肥老鼠…
- 黑马程序员//JAVA 树形结构程序事…
- HDU 1009 FatMouse' Trade(经典贪心)
- hdu 1009 FatMouse' Trade(贪心)
- hdu1009,FatMouse' Trade,经典贪心算法
- poj 1065_贪心
- Tomcat6 源文件目录树结构 说明