![](https://img-blog.csdn.net/20141125091152642?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbHVja2lseXdt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct node
{
double j,f,p;
}change[10000];
int cmp(node a,node b)
{
return a.p>b.p;
}
int main()
{
int m, n;
while(scanf("%d %d",&n,&m)!=EOF)
{
if(n==-1&&m==-1)break;
double ans=0,max=0;
int i,j;
for(i=0;i<m;i++)
{
scanf("%lf %lf",&change[i].j,&change[i].f);
change[i].p=change[i].j/change[i].f;
}
sort(change,change+m,cmp);
for(i=0;i<m;i++)
{
if(n>change[i].f)
{
ans=ans+change[i].j;
n=n-change[i].f;
}
else
{
ans=ans+change[i].p*n;
break;
}
}
printf("%.3lf\n", ans);
}
return 0;
}
此种算法最重要的思想就是 尽可能多的选择最多的,剩下的自由补充