2018-10-23 23:57

# PAT1020月饼，为什么市场需求need每次还都是20 呢？

//PAT1020V1
#include

int main(){
int n,need,i,j;
double tmpC,tmpA,tmpB,sum=0;
struct mooncake{
double a; //库存量
double b; //总售价
double c; //单价
}cake[1000];
scanf("%d %d",&n,&need);

``````//init
for(i=0;i<n;i++){
scanf("%ld",&cake[i].a);
}
for(i=0;i<n;i++){
scanf("%ld",&cake[i].b);
}
for(i=0;i<n;i++){
cake[i].c=cake[i].b/cake[i].a;  //单价
}
``````

// for(i=0;i<n;i++){
// printf("%ld %ld %f\n",cake[i].a,cake[i].b,cake[i].c);
// }

``````//sort
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(cake[i].c<cake[j].c){
//
tmpC=cake[j].c;
cake[j].c=cake[i].c;
cake[i].c=tmpC;
//
tmpB=cake[j].b;
cake[j].b=cake[i].b;
cake[i].b=tmpB;
//
tmpA=cake[j].a;
cake[j].a=cake[i].a;
cake[i].a=tmpA;
}
}
}
``````

// for(i=0;i // printf("%ld %ld %f\n",cake[i].a,cake[i].b,cake[i].c);
// }
// printf("\n%d",need);
//sale
for(i=0;i if(need>=cake[i].a){
sum+=cake[i].b;
need-=cake[i].a;
printf("\n%d %d",need,sum);
}
else if(need0){
sum+=need*cake[i].c;
need=0;
printf("\n%d %d",need,sum);
}
else if(need=0){
break;
}

}
// printf("%.2ld",sum);

}

``````
``````

need是市场需求，代码最后的for循环就是求售价总和，但是由于need每次都是20，因此出现错误

• 点赞
• 写回答
• 关注问题
• 收藏
• 邀请回答