hdu-oj 1009 FatMouse' Trade
2014-10-19 19:02
423 查看
FatMouse' Trade
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)Total Submission(s) : 7 Accepted Submission(s) : 5Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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 ofcat 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 followedby 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
分析:贪心题,先按照价值/代价的比值来排序,肯定是先买比值大的,应该注意的是float类型相除可能出现误差,而再乘以一个比较大数将使误差扩大,导致WA,因此要用double型
附代码:
#include<stdio.h>#include <algorithm>using namespace std;#define M 1010int cmp(int a,int b){return a>b;}int main(){int m,n,i,k,j[M],f[M],t1,t2;double a[M],temp,s;while(scanf("%d%d",&m,&n)&&(m!=-1||n!=-1)){s=0;for(i=0;i<n;i++){scanf("%d%d",&j[i],&f[i]);a[i]=(double)j[i]/f[i];}//sort(a,a+n,cmp);for(i=0;i<n-1;i++)for(k=0;k<n-1-i;k++)if(a[k]<a[k+1]){temp=a[k],t1=j[k],t2=f[k];a[k]=a[k+1],j[k]=j[k+1],f[k]=f[k+1];a[k+1]=temp,j[k+1]=t1,f[k+1]=t2;}for(i=0;i<n;i++){if(m>f[i]){s+=j[i];m=m-f[i];}else{s+=(double)m*j[i]/f[i];break;}}printf("%.3lf\n",s);}return 0;}
相关文章推荐
- 【HDU-oj]-1009-FatMouse' Trade(贪心)
- hdu 1009 FatMouse' Trade
- HDU1009 FatMouse' Trade
- 一入贪心深似海 ~ HDU 1009 FatMouse' Trade
- HDU 1009 FatMouse' Trade
- 文章标题 HDU 1009 : FatMouse' Trade(贪心)
- HDU 1009 FatMouse' Trade
- hdu-1009 FatMouse' Trade
- HDU 1009 FatMouse' Trade
- HDU 1009 FatMouse' Trade (贪心、结构体排序)
- HDU 1009 FatMouse' Trade (贪心)
- HDU 1009 FatMouse' Trade (贪心)
- HDU 1009 FatMouse' Trade 贪心水题
- hdu-1009-贪心-FatMouse' Trade
- HDU 1009 FatMouse' Trade 简单贪心,细节处理
- HDU 1009 FatMouse' Trade(贪心)
- hdu 1009 FatMouse' Trade
- HDU_1009 FatMouse' Trade 【贪心】
- HDU--1009 -- FatMouse' Trade
- HDU - 1009 FatMouse' Trade 贪心