hdu 1009 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
--------------------------------------------------------------------------------
别的人说,qsort在这里的排序结果不稳定,刚开始,我也不知道到底怎么回事,我就先用了sort提交一次,结果正确;然后我用qsort提交了一次,发现结果也是正确的;
总体思路还是排序,不过是用 a/c排序的 ;
PS : 还有这里给出的是 j[i] 和 F[i]; 而不是 一斤 F[i]可以换多少 javabean(和拿道“看来老人真的饿了”,题不同) 所以只需要判断M是否大于每一组的F[i].c,够买 就买下M-=F[i].c,bean += F[i].a,然后买下一种;不够买完 就买其中的一部分;
而老人那道题,给出的是单价,所以要有一个循环即 当现有的钱够买某种玉米时,就买一斤,如果钱够,在循环中将这种玉米卖完;
这是这类题的两种情况,1.给出的是两种物品的 价值(暂且这么表示,懂就行),需要求单价;2.是给出单价;
#include<stdio.h> #include<string.h> #include<stdlib.h> //#include<algorithm> //using namespace std; struct trade { int a,c; double av; }Fat[1010]; //bool cmp(trade a, trade b) //{ // return a.av > b.av; //} int main() { int cmp( const void *a ,const void *b); int M,N,i; double bean; while(scanf("%d%d",&M,&N)&&M!=-1&&N!=-1) { bean = 0; memset(Fat,0,sizeof(Fat)); for(i=0;i<N;i++)  scanf("%d%d",&Fat[i].a,&Fat[i].c); for(i=0;i<N;i++) Fat[i].av = (double)Fat[i].a / (double)Fat[i].c; qsort(Fat,N,sizeof(Fat[0]),cmp); //sort(Fat,Fat+N,cmp); //for(i=0;i<N;i++) // printf("%.2f %4d %4d\n",Fat[i].av,Fat[i].a,Fat[i].c); for(i=0;i<N&&M>0;i++) { if(M>=Fat[i].c) { bean += Fat[i].a; M -= Fat[i].c; } else { bean += M * Fat[i].av; //M -= Fat[i].c; M=0; break; } } printf("%.3lf\n",bean); } return 0; } int cmp( const void *a ,const void *b) { return  (*(struct trade*)a).av < (*(struct trade*)b).av ? 1 : -1; }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值