总体思路就是按单价排序,然后先卖贵的
#include<stdio.h>
#include<stdlib.h>
typedef struct mcake{
double storage;
double price;
}mcake;
int compare(const void *a,const void *b){
mcake c=*(mcake *)a;
mcake d=*(mcake *)b;
return d.price/d.storage > c.price/c.storage ? 1 : -1;
}
int main(){
int n,d;
double count=0.0;
scanf("%d %d",&n,&d);
mcake cakes[n];
for(int i=0;i<n;i++)
scanf("%lf",&cakes[i].storage);
for(int i=0;i<n;i++)
scanf("%lf",&cakes[i].price);
qsort(cakes,n,sizeof(cakes[0]),compare);
for(int i=0;i<n;i++){
if(cakes[i].storage<=d){
count+=cakes[i].price;
d-=cakes[i].storage;
}
else if(cakes[i].storage>d){
count+=cakes[i].price * d/cakes[i].storage;
break;
}
}
printf("%.2lf",count);
return 0;
}
1.是要按单价排序,开始写成按售价排序,逻辑错误了,很致命
2.定义成double类型,正数和正整数还是不一样的,如果定义成int型,有个测试点不过(别问我为什么知道)
|