2134 Problem E FatMouse's Trade

问题 E: FatMouse's Trade

时间限制: 1 Sec  内存限制: 32 MB

题目描述

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. 

输入

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.

输出

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.

样例输入

4 2
4 7
1 3
5 5
4 8
3 8
1 2
2 5
2 4
-1 -1

样例输出

2.286
2.500

经验总结

题意是这样的,肥老鼠准备了M磅猫粮,用于跟猫做交易,让猫看守存储他最喜爱的食物Java豆。仓库有N个房间,第i个房间有J[i]磅Java豆,需要F[i]磅猫粮。肥老鼠不用获得所有房间的Java豆,相反,他每支付F[i]*a%磅的猫粮也许可以获得j[i]*a%磅的Java豆,现在请你告诉他他可以获得最多Java豆的数量。
其实可能就是读英语比较困难,理解题目意思,那么这题就很好做了,每个房间的回报率不同,先根据回报率排个序(回报率=J[i]/F[i] ,每磅猫粮可以获得多少磅Java豆),然后贪心方式进行累加,最后输出即可~

AC代码

#include 
#include 
using namespace std;
struct room
{
	double javabean,food,profitrate;
}r[1010];
bool cmp(room a,room b)
{
	return a.profitrate>b.profitrate;
}
int main()
{
	double sumfood;
	int n;
	while(~scanf("%lf %d",&sumfood,&n))
	{
		if(n==-1)   break;
		for(int i=0;i=r[i].food)
			{
				sumfood-=r[i].food;
				lastp+=r[i].javabean;
			}
			else if(sumfood>0&&sumfood

 

你可能感兴趣的:(codeup,FatMouse's,Trade,2134,codeup,C++)