结合结构体,贪心,浮点数做分母可为零。贪心的标准要选对。
#include<stdio.h>
#include<algorithm>
using namespace std;
struct note {
double java;
double food;
double ratio;
} s[1001];
bool judge(note a,note b) {
return a.ratio>b.ratio;
}
int main() {
double n,m;
while(scanf("%lf %lf",&n,&m)&&n!=-1&&m!=-1) {
double sum=0;
for(int l=0; l<m; l++) {
scanf("%lf %lf",&s[l].java,&s[l].food);
s[l].ratio=s[l].java/s[l].food;//此处标记
}
sort(s,s+(int)m,judge);
for(int l=0; l<m; l++) {
if(n>=s[l].food) {
sum=sum+s[l].java;
n=n-s[l].food;
} else {
sum=sum+n*s[l].ratio;
break;
}
}
printf("%.3lf\n",sum);
}
return 0;
}