FatMouse' Trade hdu 1009

来源:互联网 发布:电视机顶盒网络设置 编辑:程序博客网 时间:2024/06/03 15:49

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 37 24 35 220 325 1824 1515 10-1 -1

Sample Output

13.333

31.500

该题是一个贪心的入门题。 题目的意思我好久才明白(英语菜菜)。 就是说 ,你 用m元钱买猫粮,不能重复买, 

所以 就遇到了一个选择的问题,,所以,我们把每个的单价算出来,然后排序, 这样就可以从性价比高的开始买了。

#include<stdio.h>#include<math.h>double s[1010];double  a[1010];double v[1010];int main(){int n,m,i,j,k;while(~scanf("%d%d",&m,&n)){for(i=0;i<n;i++){scanf("%lf%lf",&s[i],&a[i]);}if(n==-1&&m==-1) break;for(i=0;i<n;i++){v[i]=s[i]/a[i];}for(i=0;i<n;i++){for(j=0;j<n-i-1;j++){if(v[j]<v[j+1]){double t=v[j];v[j]=v[j+1];v[j+1]=t; t=s[j];s[j]=s[j+1];s[j+1]=t; t=a[j];a[j]=a[j+1];a[j+1]=t;}}}double sum=0;for(i=0;i<n;i++){if(m-a[i]>=0){sum+=s[i];m-=a[i];}else if(m-a[i]<0&&m>0){sum+=v[i]*m;m=0;}if(m==0)  break;}printf("%.3lf\n",sum);}return 0;}


0 0