FatMouse' Trade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 59851 Accepted Submission(s): 20095
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.
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
Author
CHEN, Yue
Source
Recommend
JGShining
使用格式输入输出的格式说明符%d,%f,%lf等,应当注意与int,float,double等类型的对应关系。
1 #include <cstdio>
2 #include <algorithm>
3 using namespace std;
4
5 const int MAX=1050;
6 struct Room{
7 double j;
8 double f;
9 double w;
10 }r[MAX];
11
12 bool compare(const Room &r1,const Room &r2)
13 {
14 if(r1.w>r2.w)
15 return true;
16 else
17 return false;
18 }
19
20 int main()
21 {
22 double m,ans,jv,fv;
23 int n;
24 while(scanf("%lf%d",&m,&n)==2&&n!=-1)
25 {
26 ans=0;
27 for(int i=0;i<n;i++)
28 {
29 scanf("%lf%lf",&jv,&fv);
30 r[i].j=jv;
31 r[i].f=fv;
32 r[i].w=jv/fv;
33 }
34 sort(r,r+n,compare);
35
36 for(int i=0;i<n;i++)
37 {
38 if(m>=r[i].f)
39 {
40 m-=r[i].f;
41 ans+=r[i].j;
42
43 }
44 else
45 {
46 ans+=r[i].w*m;
47 break;
48 }
49 }
50 printf("%.3lf
",ans);
51 }
52 }