日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

【HDU - 1009 】FatMouse' Trade (贪心)

發(fā)布時(shí)間:2023/12/10 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【HDU - 1009 】FatMouse' Trade (贪心) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題干:

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 -1

Sample 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)告:

? 赤果果的貪心,,排序后先買(mǎi)最值得買(mǎi)的,直到錢(qián)花光了。

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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。