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

 

也是ACM题目,用结构体数组做的。

View Code
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 typedef struct{
 4     float get;
 5     float pay;
 6     float sortNum;
 7 }fatCat;
 8 int main()
 9 {
10     int M,N,i;
11     fatCat *array;
12     float final;
13     fatCat* sort(fatCat* array,int N);
14     float calculate(fatCat *array,float M,int N);
15     printf("input the M and N:\n");
16     scanf("%d%d",&M,&N);
17     array=(fatCat*)malloc(N*sizeof(fatCat));
18     printf("plz input the numbers:\n");
19     for(i=0;i<N;i++)
20     {
21         scanf("%f",&array[i].get);
22         scanf("%f",&array[i].pay);
23         array[i].sortNum=array[i].get/array[i].pay;
24     }
25     array=sort(array,N);
26     final=calculate(array,M,N);
27     printf("the final is :%f\n",final);
28 }
29 fatCat* sort(fatCat* array,int N)
30 {
31     int p,j;
32     fatCat tmp;
33     for(p=1;p<N;p++)
34     { 
35         tmp=array[p];
36         for(j=p;j>0&&array[j-1].sortNum>tmp.sortNum;j--)
37             array[j]=array[j-1];
38         array[j]=tmp;
39     }
40     return array;
41 }
42 float calculate(fatCat *array,float M,int N)
43 {
44     float total=0;
45     int i=N-1;
46     for(;i>=0;i--)
47     {
48         if(M>=array[i].pay)
49         {
50             M=M-array[i].pay;
51             total=total+array[i].get;
52             continue;
53         }
54         else
55         {
56             total=total+(M/array[i].pay)*array[i].get;
57             break;
58         }
59     }
60     printf("%f\n",total);
61     return total;
62 }
posted @ 2012-09-27 11:55  长溪  阅读(215)  评论(0编辑  收藏  举报