C - FatMouse' Trade

來源:互聯網
上載者:User

標籤:des   style   blog   ar   io   color   sp   for   java   

FatMouse‘ Trade

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 45965    Accepted Submission(s): 15395


Problem DescriptionFatMouse 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. 

 

InputThe 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. 

 

OutputFor 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 Input5 37 24 35 220 325 1824 1515 10-1 -1 

 

Sample Output13.33331.500  首先這是一道貪心演算法,當f/j越大時代表這樣越有利,所以進行排序;但也有值得注意的幾組資料(來自杭電的大神)
此題除了要滿足例子以外,還要滿足一些條件才能真正算ac:0 11 01.0001 00.0005 410000 52000 2100 0300 010400.000資料類型用double,就這樣
 1 #include<stdio.h> 2 int j[1010],f[1010]; 3 double a[1010]; 4 void paixu(double a[],int k[],int f[],int n) 5 { 6     int i=0,j=n-1,h=k[0],m=f[0]; 7     double  t=a[0]; 8     if(n>1){ 9         while(i<j)10         {11             for(;i<j;j--)12             if(a[j]>t){13                 a[i]=a[j];14                 f[i]=f[j];15                 k[i]=k[j];16                 i++;17                 break;18             }19             for(;i<j;i++)20             {21                 if(a[i]<t){22                     a[j]=a[i];23                     f[j]=f[i];24                     k[j]=k[i];25                     j--;26                     break;27                 }28             }29         }30         a[i]=t;31         f[i]=m;32         k[i]=h;33         paixu(a,k,f,i);34         paixu(a+i+1,k+i+1,f+i+1,n-i-1);35     }36 }37 void hanshu(int m,int n)38 {39     int i;40     double  sum=0;41     for(i=0;i<n;i++)42     {43         if(m>=f[i]){44             sum=sum+j[i];45             m=m-f[i];46         }47         else{48             sum=sum+1.0*m/f[i]*j[i];49             break;50         }51     }52     printf("%0.3f\n",sum);53 }54 int main()55 {56     int n,m,i,t;57     while(1)58     {scanf("%d%d",&m,&n);59     if(m==-1&&n==-1)break;60     for(i=0;i<n;i++)61         {scanf("%d%d",&j[i],&f[i]);62         if(f[i]!=0)a[i]=1.0*j[i]/f[i];63         else a[i]=1020;64         }65         paixu(a,j,f,n);66         hanshu(m,n);67         }68     return 0;69 70 }

 

 

C - FatMouse' Trade

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.