FatMouse’ Trade
原题链接
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 95691 Accepted Submission(s): 33343
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
题解:我阅读理解是真的差,借助翻译半天才读懂题,这个题的大体意思就是,F耗子要用M磅猫粮来找J猫买食物,每JI磅食物要用FI磅猫粮来换,问怎样获得最大的食物(食物可以取百分比)说下我的解题思路,很简单,把每种食物和猫粮的比例算出来(JI/FI)然后降序排序,从前往后取,贪就完事了,能全买就全买,不能全买就取百分比。下面是AC代码
#include"iostream"
#include"algorithm"
using namespace std;
struct str
{
int JI;//能换取的食物
int FI;//需要的猫粮
double porportion;//比例(JI/FI)
}Room[1007];
bool cmp(struct str a,struct str b)
{
return a.porportion>b.porportion;
}
int main()
{
int n,m;
while(1)
{
cin >> m >> n;
if(n == -1 && m == -1)
break;
for(int i=0;i<n;i++)
{
cin >> Room[i].JI >> Room[i].FI;
Room[i].porportion=(double)Room[i].JI/Room[i].FI;//算出比例
}
sort(Room,Room+n,cmp);//按比例降序排序
double s=0;//存储已经换的的食物数量
for(int i=0;i<n;i++)
{
//printf("%.3f ",Room[i].porportion);
if(m == 0)//猫粮用完时,退出
break;
if(m >= Room[i].FI)//如果能全换就全换,不能全换就换百分比
{
s+=Room[i].JI;
m-=Room[i].FI;
}
else
{
s=s+m*Room[i].porportion;
m=0;
}
}
printf("%.3f\n",s);
}
return 0;
}