求解一道ACM的题

yixiayizi yixiayizi 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
题目:
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
求助错哪里了,或者哪里考虑不周全了









...全文
评论 6
收藏 已收藏
分享
返回顶部
yixiayizi
C++ 语言 65,214 成员 250,514 内容