3,881
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
struct Item
{
int J, F;
double ratio;
};
int cmp(void *a, void *b )
{
struct Item *p1, *p2;
p1=(struct Item*)a;
p2=(struct Item*)b;
if((p1->ratio)-(p2->ratio)<=0)
return 1;
else
return 0;
}
int main()
{
int m, n, i;
double M;
struct Item item[1000];
scanf("%d %d", &m, &n);
while(m!=-1 && n!=-1)
{
M=0;//初始老鼠得到的食物为0
if(n!=0) // room不为0的时候
{
for(i=0; i<n; i++)
{
scanf("%d %d", &item[i].J, &item[i].F);
if(item[i].F==0)
{
M+=item[i].J;
item[i].F=-1;
}
item[i].ratio=((double)item[i].J)/((double)item[i].F);
}
qsort(item,n,sizeof(struct Item),cmp);
i=0;
while(i<n)
{
if(item[i].F!=-1)
{
if(m>=item[i].F)
{
M+=item[i].J;
m-=item[i].F;
}
else
{
M+=item[i].ratio*(double)m;
break;
}
}
i++;
}
}
printf("%.3lf\n", M);
scanf("%d %d", &m, &n);
}
return 1;
}