FatMouse' Trade |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
Total Submission(s): 2878 Accepted Submission(s): 888 |
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 |
Author
CHEN, Yue
|
Source
ZJCPC2004
|
Recommend
JGShining
|
有N个房间,你有M镑的猫粮,在第i个房间你最多可以花费F[i]的猫粮来交换J[i]的豆子,交换可以按比例来,不一定全部交换,能在多个房间交换。求M镑最多能交换多少豆子。
下面是代码,费了我好长时间,一直RA,最后发现问题出在使用while循环时没有判定i < n;以后可得记住,不要再犯这些小细节问题%……
代码:
#include<stdio.h>
#include<malloc.h>
typedef struct num
{
int a, b;
double avg;
}Num;
int main(void)
{
int m, n;
int i, j;
int max;
Num temp;
double sum;
Num *num1;
while(scanf("%d%d", &m, &n) != EOF && (m != -1 || n != -1))
{
getchar();
num1 = (Num*)malloc(sizeof(Num) * n);
for(i = 0;i < n;i++)
{
scanf("%d%d", &(num1[i].a), &(num1[i].b));
getchar();
num1[i].avg = (double)(num1[i].a) / (double)num1[i].b;
}
for(i = 0;i < n - 1;i++)
{
max = i;
for(j = i + 1;j < n;j++)
{
if(num1[j].avg > num1[max].avg)
max = j;
}
if(max != i)
{
temp = num1[i];num1[i] = num1[max];
num1[max] = temp;
}
}
sum = 0;
i = 0;
while(m >= num1[i].b && i < n)
{
m -= num1[i].b;
sum += num1[i].a;
i++;
}
sum += num1[i].avg * m;
printf("%.3lf\n", sum);
free(num1);
}
return 0;
}