背包。
#include <bits/stdc++.h>
using namespace std;
struct node{
double kc,sj;
double avg;
}s[1010];
bool cmp(node n1,node n2){
return n1.avg>n2.avg;
}
int main(){
int n,total;
cin>>n>>total;
for(int i=0;i<n;i++){
cin>>s[i].kc;
}
for(int i=0;i<n;i++){
cin>>s[i].sj;
double avs=1.0*s[i].sj/s[i].kc;
s[i].avg=avs;
}
sort(s,s+n,cmp);
int rest=total;//剩余的月饼量
double money=0;
int idx=0;
for(int i=0;i<n;i++){
if(rest-s[i].kc>=0){
rest-=s[i].kc;
money+=s[i].sj;
}else{
money=money+s[i].avg*rest;
break;
}
}
printf("%.2f",money);
return 0;
}
标签:kc,月饼,int,money,003,sj,L2,double,avg
From: https://www.cnblogs.com/chengyiyuki/p/18072957