hdu 1009 FatMouse' Trade sort结构体

    最近杭电的数据很阴险了,

FatMouse' Trade,没过的同学测这组数据:

  1. 0 3 
  2. 100 0 
  3. 100 0 
  4. 100 0 
  5.  
  6. Answer  
  7. 300.000 

  1 #include<iostream>

 2 #include<algorithm>
 3 #include<stdio.h>
 4 using namespace std;
 5 struct  node
 6 {
 7         int j,f;
 8         double a;    
 9 };
10 
11 node test[1005];
12 bool comp(node a1,node a2)
13 {
14      if(a1.a!=a2.a)
15          return a1.a>a2.a;
16      else
17          return 0;
18 }
19 int main()
20 {
21     int m,i,n;
22     
23     double sum;
24     while(scanf("%d%d",&m,&n),m!=-1,n!=-1)
25     {
26        
27           
28       sum=0;
29       for(i=0;i<n;i++)
30       {
31           scanf("%d%d",&test[i].j,&test[i].f);
32           if(test[i].j==0)
33               test[i].a=0;
34           else if(test[i].f==0)
35                test[i].a=static_cast<double>(1001);
36           else
37               test[i].a=double(test[i].j)/test[i].f;
38       }
39       
40       sort(test,test+n,comp);
41       
42       for(i=0;i<n;i++)
43       {
44                 
45                 if(m-test[i].f>=0)
46                 {
47                     sum+=test[i].j;
48                     m-=test[i].f;
49                 }
50                 else
51                 {
52                     sum+=test[i].a*m;
53                     m=0;
54                     
55                 }
56                 
57       }
58       printf("%.3f\n",sum);
59     }
60     return 0;
61 }
原文地址:https://www.cnblogs.com/anderson0/p/2039777.html