FatMouse’ Trade

Problem Description
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.

Input
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1’s. All integers are not greater than 1000.

Output
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.

Sample Input
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1

Sample Output
13.333
31.500

题目大意:就算说给你金额m去n个小仓库里面兑换食物,每个小仓库的价格不同,然后要我们找到让他买食物最多的方法.
思路:贪心,拿样例3说,25 18 可以这么理解我要拿到25样食物,我需要要花费18元,25/18=1.38 就算说我拿一块钱大约可以买个1.38个食物,所以我们当然希望这里比例越大越好,直接做一个降序,然后慢慢买,知道买不了的时候停止,最后买到的就算食物最多的方法.
ac代码(c++):

#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<iomanip>
struct t{
	int a;
	int b;
	double rate;
};
struct t tan1[1005];
using namespace std;
bool cmp(struct t a,struct t b){
	return a.rate>b.rate;
}
int main()
{
	int a,b;
	while(cin>>a>>b)
	{
		memset(tan1,0,sizeof(tan1));
		if(a==-1&&b==-1)
		break;
		for(int i=0;i<b;i++){
			cin>>tan1[i].a>>tan1[i].b;
			tan1[i].rate=(double)1.0*tan1[i].a/(double)tan1[i].b;
		}
		sort(tan1,tan1+b,cmp);
		double sum=0;
		for(int i=0;i<b;i++){
			if(a>=tan1[i].b){
				a-=tan1[i].b;
				sum+=tan1[i].a;
			}else{
				sum+=a*tan1[i].rate;
				break;
			}
		}
		cout<<fixed<<setprecision(3)<<sum<<endl; 
	}
}