
两个条件貌似缺一不可
不明白为什么不能是sum+=(s[i].value*s[i].cat_food);
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1010;
typedef struct
{
int java_bean;
int cat_food;
double value;
}S;
S s[maxn];
bool cmp(S a,S b)
{
return a.value>b.value;
}
int main ()
{
int M,N,i,total;
double sum;
while(scanf("%d%d",&M,&N)!=EOF)//M份猫粮
{
if(M==-1 && N==-1)
break;
for(i=0;i<N;i++)
{
scanf("%d%d",&s[i].java_bean,&s[i].cat_food);
s[i].value=1.0*s[i].java_bean/s[i].cat_food;//一份cat_food可以换多少java_bean
}
sort(s,s+N,cmp);
i=0;
total = M;
sum=0;
while(total>0 && i<N)
{
if(total >= s[i].cat_food)
{
sum+=s[i].java_bean;
total-=s[i].cat_food;
}
else
{
sum+=(s[i].value*total);
total=0;
}
i++;
}
printf("%.3lf\n",sum);
}
return 0;
}
568


。&spm=1001.2101.3001.11974&articleId=84894085&d=1&t=3&u=8096ff64c48b46048ad75438e9c791c4)

被折叠的 条评论
为什么被折叠?



