FatMouse' Trade(hdoj1009)

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 -1
 
Sample Output
13.333
31.500
 1 #include<stdio.h>/**此题有很多边缘数据没有想到,或者压根就没有去想/ 
 2 #include<algorithm>
 3 using namespace std;
 4 typedef struct
 5 {
 6     double x,y;/*数据类型错点*/
 7     double z;
 8 }node;
 9 int cmp(node p,node q)
10 {
11     return p.z>=q.z;
12 }
13 node a[1002];
14 int main()
15 {
16     double M;
17     int N;
18     while(~scanf("%lf%d",&M,&N))
19     {
20         int i,j;
21         double max=0,shen=M;
22         if(M==-1&&N==-1)
23             break;
24         if(N==0)/*room为0情况*/ 
25             {
26                 printf("0.000
");
27                 continue;
28             }
29         else
30         {
31             for(i=0;i<N;i++)
32                 {
33                     scanf("%lf%lf",&a[i].x,&a[i].y);
34                     a[i].z=a[i].x/a[i].y;
35                 }
36             sort(a,a+N,cmp);
37             /*for(i=0;i<N;i++)
38                 printf("%d %d  %lf
",a[i].x,a[i].y,a[i].z);*/
39             for(i=0;;i++)
40             {
41                 if(shen>a[i].y)
42                 {
43                     max+=a[i].x;
44                     shen=shen-a[i].y;
45                 }    
46                 else if(shen==a[i].y)/*可能漏掉,因为存在y=0情况,此情况不能和else并在一起,看了讨论组才知道的*/ 
47                 {
48                     max+=a[i].x;
49                     shen=shen-a[i].y;
50                 }    
51                 else
52                 {
53                     max+=(shen/a[i].y)*a[i].x;
54                     break;
55                 }
56             }
57         }
58         printf("%.3lf
",max);
59     }
60 } 
 
原文地址:https://www.cnblogs.com/a1225234/p/4497284.html