求解一道ACM的题

yixiayizi 2008-02-11 10:45:07
题目:
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
求助错哪里了,或者哪里考虑不周全了









...全文
134 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zac372424332 2010-02-11
  • 打赏
  • 举报
回复
我也弄了好久没弄出来,后来把float改成double就行了,谢谢楼主。
visame 2008-02-12
  • 打赏
  • 举报
回复
又想了一会儿,肯定是

存在j=0的数据,而你又图简单把st[i].info=1000000000;造成结果偏大。
比较的时候就要多写两句而已。

result=result+st[i].info*m;//here,最后应该不增加,但你的增加了
break;
visame 2008-02-12
  • 打赏
  • 举报
回复
看了一遍,思路很清晰。比我的还好,不过我主要是练习一下合并排序。
应该是浙江的。
可能出错的两个地方:

            if(st[i].j==0)
st[i].info=1000000000;//此数太小,换成更大的,测试数据一般超级反常
else
st[i].info=float(st[i].f)/float(st[i].j);//这里把float换成double提高精度


Try again!Waiting for your good news!
visame 2008-02-12
  • 打赏
  • 举报
回复
经典的贪心算法。
#include <iostream>
using namespace std;

void Merge(double *Cat,double *Mouse,int start,int middle,int end)
{
double *TempCat=new double [end-start+1];
double *TempMouse=new double [end-start+1];
int i=start,j=middle+1,marker=0;
while (i<=middle&&j<=end)
{
while (Cat[i]/Mouse[i]<=Cat[j]/Mouse[j]&&i<=middle&&j<=end)
{
TempCat[marker]=Cat[i];
TempMouse[marker]=Mouse[i];
++marker;
++i;
}
while (Cat[j]/Mouse[j]<=Cat[i]/Mouse[i]&&i<=middle&&j<=end)
{
TempCat[marker]=Cat[j];
TempMouse[marker]=Mouse[j];
++marker;
++j;
}
}
while (i<=middle)
{
TempCat[marker]=Cat[i];
TempMouse[marker]=Mouse[i];
++marker;
++i;
}
while (j<=end)
{
TempCat[marker]=Cat[j];
TempMouse[marker]=Mouse[j];
++marker;
++j;
}
for (int m=start;m<=end;++m)
{
Cat[m]=TempCat[m-start];
Mouse[m]=TempMouse[m-start];
}
delete []TempCat;
delete []TempMouse;
}
void MergeSort(double *Cat,double *Mouse,int start,int end)
{
if (start<end)
{
int middle=(start+end)/2;
MergeSort(Cat,Mouse,start,middle);
MergeSort(Cat,Mouse,middle+1,end);
Merge(Cat,Mouse,start,middle,end);
}
}

int main()
{
int N;
double M;
while(scanf("%lf%d",&M,&N)!=EOF&&N!=-1)
{
double *Cat=new double [N];
double *Mouse=new double [N];
for (int i=0;i<=N-1;++i)
{
scanf("%lf%lf",&Mouse[i],&Cat[i]);
}
MergeSort(Cat,Mouse,0,N-1);
int j=0;
double Result=0;
for (j=0;j<=N-1;++j)
{
if (M-Cat[j]>=0)
{
M=M-Cat[j];
Result+=Mouse[j];
}
else
{
Result+=M*Mouse[j]/Cat[j];
printf("%.3f\n",Result);
break;
}
}
}
}
zhangpingfly 2008-02-12
  • 打赏
  • 举报
回复
这个用的就是贪心~~~
下面是我在杭电ACM上的answer
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
int main()
{
int m,n,i,j,num;
int residual,a[2002];
double max,sum,b[1000];
while (cin>>m>>n)
{
sum=0;residual=m;num=n;
if(m==-1&&n==-1)exit(0);
for (i=0;i<n*2;i++)
scanf("%d",&a[i]);
for(i=0,j=0;i<n;i++,j=j+2)
b[i]=(double)a[j]/a[j+1];
while (residual>0&&num>0)
{
max=b[0];j=0;
for (i=1;i<n;i++)
{
if(b[i]>max)
{
max=b[i];
j=i;
}
}
if(residual>=a[j*2+1])
{
residual-=a[j*2+1];
sum+=a[j*2];
b[j]=0;
}
else
{
sum=sum+(double)residual*a[j*2]/a[j*2+1];
residual=0;
}
num--;
}
printf("%.3f\n",sum);
}
return 0;
}
yixiayizi 2008-02-12
  • 打赏
  • 举报
回复
把float类型都改成了double类型,就都过了。汗死~~~~

st[i].info=1000000000;
这个整数不是最大了吗,你的意思是换成长整型吗?

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧