求解一道ACM的题
题目:
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
我的程序:
通不过测试,显示是:wrong answer
求助错哪里了,或者哪里考虑不周全了
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<iostream>
using namespace std;
typedef struct node
{
int f,j;
float info;
}node;
int main()
{
int m,n,i,j;
node st[2000],temp;
float result;
while(cin>>m>>n&&m!=-1&&n!=-1)
{
result=0;
for(i=0;i<n;i++)
{
cin>>st[i].f>>st[i].j;
if(st[i].j==0)
st[i].info=1000000000;
else
st[i].info=float(st[i].f)/float(st[i].j);
}
for(i=0;i<n;i++)
for(j=0;j<n-1-i;j++)
{
if(st[j].info<st[j+1].info)
{
temp=st[j];
st[j]=st[j+1];
st[j+1]=temp;
}
}
i=0;
while(i<n)
{
if(m>=st[i].j)
{
result=result+st[i].f;
m=m-st[i].j;
i++;
}
else
{
result=result+st[i].info*m;
break;
}
}
printf("%0.3f\n",result);
}
return 0;
}
通不过测试,显示是:wrong answer
求助错哪里了,或者哪里考虑不周全了
...全文
相关推荐
大二做的算法课设,题目已经忘了是什么了,只记得是一道acm题,用双向动
大二做的算法课设,题目已经忘了是什么了,只记得是一道acm题,用双向动态规划求解的。这里用C++解决_Algorithm-Course-Set
PKU acm 1000题-2000题
北大1000题至2000部分题的源代码...
a-acm-problm.rar_acm题eazzzzzy
acm一个题 我们初赛赛场上的一道题。做了1个多小时。比较简单。
北大ACM题解答源代码
这里集中了北大ACM竞赛的许多源代码,对ACM学习者会有帮助的。
acm1003题答案
这是acm上第1003题的答案,是通过网站测试的答案,可供参考。


