HDU 1009FatMouse' Trade
生活随笔
收集整理的這篇文章主要介紹了
HDU 1009FatMouse' Trade
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
FatMouse' Trade
Time Limit : 2000/1000ms (Java/Other)???Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 50???Accepted Submission(s) : 19
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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.500Author
CHEN, YueSource
ZJCPC2004Statistic | Submit | Back 大意:老鼠有m磅貓糧,在一個地方有n個房間,每個這里有貓在看守,第i個房間里有j[i]鎊豆子要求f[i]磅貓糧房間里的豆子便可全部帶走,但老鼠不一定要全部帶走,如果老鼠給了貓f[i]*a%磅的貓糧,則他可獲得j[i]*a%磅的豆子,求老鼠可獲得的最大數(shù)量的豆子是多少? 思路:問題相當于買東西,如何花最少的錢來買最近可能多的東西。所以那件東西花的錢(貓糧)最少就先買哪一個,也就是說花同樣的錢(貓糧),哪個買的多就先買哪個。 即先買j[i]/f[i]最大的那個,若不貓要求的貓糧,則就買m/f[i]*j[i]的豆子??上葘[i]/f[i]排序,再從最多的往下買 代碼: #include <iostream> #include<iomanip> #define MAX 9999999 using namespace std; double j[1000],f[1000],p[1000]; void order(double p[],double j[],double f[],int n) {int i,s,k;for(i=n; i>0; i--){k=0;for(s=0; s<i; s++){if(p[k]>=p[s]){k=s;}}double t;t=p[k];p[k]=p[i-1];p[i-1]=t;t=j[k];j[k]=j[i-1];j[i-1]=t;t=f[k];f[k]=f[i-1];f[i-1]=t;} } int main() {int n,i; double s,m;while(cin>>m>>n&&(n!=-1&&m!=-1)){s=0;for(i=0; i<n; i++){cin>>j[i]>>f[i];if(f[i]!=0)//分母等于零的情況處理p[i]=j[i]/f[i];elsep[i]=MAX;//max假定無窮}order(p,j,f,n);//排序for(i=0; i<n; i++){if(m>=f[i])//足夠付得起{s=s+j[i];m=m-f[i];}else if(m>=0&&m<f[i])付不起時{s=s+m*p[i];m=0;}}cout<<setiosflags(ios::fixed)<<setprecision(3);cout<<s<<endl;}return 0; }
總結
以上是生活随笔為你收集整理的HDU 1009FatMouse' Trade的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 上传ipa到appstore的步骤说明
- 下一篇: Windows下搭建局域网内简易git服