Input
输入数据首先包含一个正整数C,表示有C组测试用例,每组测试用例的第一行是两个整数n和m(0<n<=1000,0<m<=1000),分别表示经费的金额和大米的种类,然后是m行数据,每行包含2个整数p和h(1<=p<=25,1<=h<=100),分别表示单价和对应大米的重量。
Output
对于每组测试数据,请输出能够购买大米的最多重量(你可以假设经费买不光所有的大米)。
每个实例的输出占一行,保留2位小数。
Sample Input
1
7 2
3 3
4 4
Sample Output
2.33
#include<stdio.h>
#include<algorithm>
using namespace std;
struct Node {
double p,h;
} str[1010];
bool cmp(Node x,Node y) {
return x.p<y.p;
}
int main() {
int n,m,T;
scanf("%d",&T);
while(T--) {
scanf("%d %d",&n,&m);
for(int i=0; i<m; i++) {
scanf("%lf %lf",&str[i].p,&str[i].h);
}
sort(str,str+m,cmp);
double sum=0.0;
for(int i=0; i<m; i++) {
if(str[i].h<=n/str[i].p) {
sum+=str[i].h;
n-=str[i].h*str[i].p;
} else {
sum+=n/str[i].p;
break;
}
}
printf("%.2lf\n",sum);
}
return 0;
}