文章
33
粉丝
78
获赞
3
访问
19.0k
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
struct goods{
double mi;
double wi;
};
bool ascd(goods a,goods b){
return a.wi/a.mi < b.wi/b.mi ;
}
int main(){
int x,n;
double total = 0;
goods g[1000];
while(cin >>x >> n){
if(x == -1 && n== -1)
return 0;
for(int i = 0;i < n;i++){
cin >>g[i].mi >>g[i].wi;
}
stable_sort(g,g + n,ascd);
int j = 0;
while(x >0&&j <n){
if(g[j].wi <= x){
total += g[j].mi;
x -=g[j].wi;
j++;
}
else{
total += x*(g[j].mi/g[j].wi);
x =0;
}
}
printf("%.3lf\n",total);
}
return 0;
}
登录后发布评论
total初始化要放在循环里,否则多组数组会出错影响到下一组数据的计算