2009年03月28日
2455 ワード
FatMouse' Trade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6312 Accepted Submission(s): 1816
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
Sample Output
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6312 Accepted Submission(s): 1816
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#include
#include
using namespace std;
int KK_WhoBig(int arry1[2], int arry2[2])
{
if(arry1[0]*arry2[1]>arry1[1]*arry2[0])
return 1;
else return 0;
}
void KK_insert(int arry[][2],int arry_temp[2],int i)
{
if(i==0)
{
arry[i][0]=arry_temp[0];
arry[i][1]=arry_temp[1];
}
for(;i>0;)
{
i--;
if(KK_WhoBig(arry[i],arry_temp))
{
arry[i+1][0]=arry_temp[0];
arry[i+1][1]=arry_temp[1];
break;
}
else
{
arry[i+1][0]=arry[i][0];
arry[i+1][1]=arry[i][1];
arry[i][0]=arry_temp[0];
arry[i][1]=arry_temp[1];
}
}
}
int main()
{
int M,N;
int arry[1000][2];
int arry_temp[2];
double sum=0;
while(cin>>M>>N&&M!=-1&&N!=-1)
{
sum=0;
for(int i=0;i>arry_temp[0]>>arry_temp[1];
KK_insert(arry,arry_temp,i);
}
for(int j=0;jarry[j][1])
{
sum+=arry[j][0];
M-=arry[j][1];
}
else if(M>0)
{
sum+=((double)(M*arry[j][0]))/((double)arry[j][1]);
M-=arry[j][1];
}
}
cout<<<<<