【HDU - 1009 】FatMouse' Trade (贪心)
題干:
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 J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* 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 J[i] and F[i] 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 -1Sample Output
13.333 31.500題目大意:
肥鼠準(zhǔn)備了 M 磅的貓糧,準(zhǔn)備和看管倉(cāng)庫(kù)的貓交易,倉(cāng)庫(kù)里裝有他最喜愛(ài)的食物 Java 豆。
倉(cāng)庫(kù)有 N 個(gè)房間。第 i 間房包含了 J[i] 磅的 Java 豆,需要 F[i] 磅的貓糧。肥鼠不必為了房間中的所有 Java 豆而交易,相反,他可以支付 F[i] * a% 磅的貓糧去交換得到 J[i] * a% 磅的 Java 豆。這里,a 表示一個(gè)實(shí)數(shù)。
現(xiàn)在他將這項(xiàng)任務(wù)分配給了你:請(qǐng)告訴他,能夠獲得的 Java 豆的最大值是多少。
注意這個(gè)‘%’是百分之!!不是取模!!!傻子了吧、、、
解題報(bào)告:
? 赤果果的貪心,,排序后先買最值得買的,直到錢花光了。
AC代碼:
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #define ll long long using namespace std; int n; double m,ans; struct Node {int j,f; } node[1005]; bool cmp(Node a,Node b) {return 1.0*a.j/a.f > 1.0*b.j/b.f; } int main() {while(cin>>m>>n) {if(n == -1 && m == -1) break;for(int i = 1; i<=n; i++) {scanf("%d%d",&node[i].j,&node[i].f);}ans=0;sort(node+1,node+n+1,cmp);for(int i = 1; i<=n; i++) {if(m >= node[i].f) {m-=node[i].f;ans+=node[i].j;}else {ans += (m/node[i].f)*node[i].j;break;}}printf("%.3lf\n",ans);}return 0 ;}?
總結(jié)
以上是生活随笔為你收集整理的【HDU - 1009 】FatMouse' Trade (贪心)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 建行10万额度信用卡办理条件 并不是人人
- 下一篇: 如何避免洗钱陷阱?生活中有哪些洗钱行为?