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个房间的粮仓,猫守的每个房间里有老鼠爱吃的javabeans,老鼠可以用猫粮交换每个房间里的javabeans,每个房间里的javabeans和需要用来交换它的猫粮不同,老鼠可以可以等比例的来用猫粮交换javabeans,比如一个房间有21个javabeans,需要3份猫粮交换,你就可以用1分猫粮来交换7个javabeans。
那么,问老鼠最多能交换多少个javabeans
思路:性价比问题,按照性价比来降序排序之后从头开始,只要猫粮还够就全部贪了他
AC代码:
#include<iostream>
#include<algorithm>
#include<iomanip>
#define MAX 10000
using namespace std;
struct trade
{
double food,cost;
}a[MAX];
bool cmp(trade x,trade y)
{
return x.food/x.cost>y.food/y.cost;
}
int main()
{
double m;
int n;
while(cin>>m>>n)
{
double ans=0;
if(m==-1||n==-1)
{
break;
}
for(int i=0;i<n;i++)
{
cin >> a[i].food>>a[i].cost;
}
sort(a,a+n,cmp);
for(int i=0;i<n;i++)
{
if(m>a[i].cost)
{
ans=ans+a[i].food;
m=m-a[i].cost;
}
else
{
ans=ans+a[i].food*(m/a[i].cost);
m=0;
break;
}
}
cout<<fixed<<setprecision(3)<<ans<< endl;
}
return 0;
}
如果你的include <bits/stdc++.h>没被屏蔽,,,,,,