题解:用struct排序月饼的平均价格,一直取最大
题目地址:https://www.nowcoder.com/questionTerminal/6fc9a928c7654b0fbc37d16b8bd29ff9
1 /** 2 * 3 *作者:Ycute 4 *时间:2019-11-01-14.30.12 5 *题目简单描述:struct排序 6 */ 7 8 9 #include<iostream> 10 #include<cmath> 11 #include<cstring> 12 #include<algorithm> 13 #include<vector> 14 using namespace std; 15 struct shop{ 16 int num; 17 int price; 18 double pva; 19 }; 20 21 bool cmp(struct shop a,struct shop b){ 22 return a.pva>b.pva; 23 } 24 25 int main(){ 26 struct shop s[1005]; 27 int t,nnum; 28 scanf("%d %d",&t,&nnum); 29 for(int i=0;i<t;i++){ 30 scanf("%d",&s[i].num); 31 } 32 for(int i=0;i<t;i++){ 33 scanf("%d",&s[i].price); 34 s[i].pva=s[i].price*1.0/s[i].num; 35 } 36 sort(s,s+t,cmp); 37 int i=0; 38 double sum=0; 39 while(nnum){ 40 if(s[i].num<=nnum){//充足情况 41 sum+=s[i].price; 42 nnum-=s[i++].num; 43 //cout<<i<<" "<<sum<<endl; 44 }else{ 45 sum+=(s[i].price*1.0/s[i].num)*nnum; 46 break; 47 } 48 } 49 printf("%.2lf\n",sum); 50 return 0; 51 }