[PAT乙] 1020 月饼 (25 分)
作者:互联网
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
struct MoonCake{
double ton;//只说是正数 没说是正整数
double price;
double one;
}mc[1010];
bool cmp(MoonCake a,MoonCake b){
return a.one>b.one;
}
int main(){
int n;
double d;
scanf("%d%lf",&n,&d);
for(int i=0;i<n;i++){
scanf("%lf",&mc[i].ton);
}
for(int i=0;i<n;i++){
scanf("%lf",&mc[i].price);
}
for(int i=0;i<n;i++){
mc[i].one=mc[i].price/mc[i].ton;
}
sort(mc,mc+n,cmp);
double count=0;
// int i=0;
// while(d>0)//浮点数的精度问题?
for(int i=0;i<n;i++){
if(d>=mc[i].ton){
d-=mc[i].ton;
count+=mc[i].price;
}else{
count+=mc[i].one*d;
break;
// d=0;
}
// i++;
}
printf("%.2f",count);
}```
标签:count,25,PAT,1020,mc,int,double,price,ton 来源: https://blog.csdn.net/weixin_48328459/article/details/123097314