C++简单程序(ACM)错误,跪求高手解答

来源:百度知道 编辑:UC知道 时间:2024/06/06 15:42:28
1009
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 amoun

你这个真的是太烦了,还没高分,代码的取名及格式也不注意而且没备注,估计没人会来帮你处理了,我把我写的给你看看吧,虽然不是比较优质的代码:

#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
struct s
{
double j;
double f;
double v;
}a[1000];
int cmp(const void *a,const void *b)
{
struct s *c=(s *)a;
struct s *d=(s *)b;
return c->v>d->v?-1:1;
}
void main()
{
int m,n;
while(scanf("%d%d",&m,&n)!=EOF)
{
if(m==-1&&n==-1) break;
for(int i=0;i<n;i++)
{
cin>>a[i].j>>a[i].f;
a[i].v=a[i].j/a[i].f;
}
qsort(a,n,sizeof(a[0]),cmp);
double sum=0;
int num=0;
for(int i=0;i<n;i++)
{
num+=a[i].f;
if(num==m) { sum+=a[i].j;break;}
if(num>m) { num-=a[i