在此对 曾经 努力参加 救援的人 致以深深的敬意 .
这一道题 挺简单的 就是简单的 结构体+贪心 而已
不过 用英文 注释 是一个 很大的 进步 , 以后 要习惯
http://acm.hdu.edu.cn/showproblem.php?pid=2187
对于幸存的灾民来说,最急待解决的显然是温饱问题,救灾部队一边在组织人员全力打通交通,一边在组织采购粮食。现在假设下拨了一定数量的救灾经费要去市场采购大米(散装)。如果市场有m种大米,各种大米的单价和重量已知,请问,为了满足更多灾民的需求,最多能采购多少重量的大米呢?
输入:
输入数据首先包含一个正整数C,表示有C组测试用例,每组测试用例的第一行是两个整数n和m(0<n<=1000,0<m<=1000),分别表示经费的金额和大米的种类,然后是m行数据,每行包含2个整数p和h(1<=p<=25,1<=h<=100),分别表示单价和对应大米的重量。
输出:
对于每组测试数据,请输出能够购买大米的最多重量(你可以假设经费买不光所有的大米)。
每个实例的输出占一行,保留2位小数。
Sample Input
1 7 2 3 3 4 4
Sample Output
2.33
下面附上 水水的 代码
1 #include<stdio.h> 2 #include<algorithm> 3 using namespace std; 4 struct rice 5 { 6 int p,h; // The price and types of rice . 7 }; 8 bool cmp(rice a,rice b) 9 { 10 return a.p<b.p; 11 } 12 int main() 13 { 14 rice a[1010]; 15 int i,m,t; 16 double n,sum; 17 scanf("%d",&t); 18 while(t--) 19 { 20 scanf("%lf%d",&n,&m); // The total amount and types of rice . 21 for(sum=i=0;i<m;i++) 22 { 23 scanf("%d%d",&a[i].p,&a[i].h); 24 } 25 sort(a,a+m,cmp); // According to the price of the target were sorted in ascending order. 26 for(i=0;i<m;i++) 27 { 28 if(a[i].p*a[i].h<=n) //First determine whether this kind of buying meters 29 { 30 sum+=a[i].h; // if we can do it 31 n-=a[i].p*a[i].h; 32 } 33 else // if we fail to do it 34 { 35 n/=a[i].p; //With the rest of the money divided by the price . 36 sum+=n; 37 break; 38 } 39 } 40 printf("%.2lf ",sum); 41 } 42 return 0; 43 }