简单的结构体和排序应用题
1 #include<iostream> 2 #include<stdio.h> 3 #include<algorithm> 4 using namespace std; 5 struct node 6 { 7 double sum,zongjia,danjia; 8 }yuebing[1005]; 9 bool cmp(node a,node b) 10 { 11 return a.danjia>b.danjia; 12 }; 13 int main() 14 { 15 int n,maxn; 16 cin>>n>>maxn; 17 int i; 18 for(i=0;i<n;i++) 19 { 20 cin>>yuebing[i].sum; 21 } 22 for(i=0;i<n;i++) 23 { 24 cin>>yuebing[i].zongjia; 25 } 26 for(i=0;i<n;i++) 27 { 28 yuebing[i].danjia=yuebing[i].zongjia/yuebing[i].sum; 29 } 30 sort(yuebing,yuebing+n,cmp); 31 double res=0.0; 32 for(i=0;i<n;i++) 33 { 34 if(maxn<=0)break; 35 if(maxn>=yuebing[i].sum) 36 { 37 maxn-=yuebing[i].sum; 38 res+=yuebing[i].zongjia; 39 } 40 else if(maxn<yuebing[i].sum) 41 { 42 res+=maxn*yuebing[i].danjia; 43 maxn=0; 44 } 45 } 46 printf("%.2lf ",res); 47 return 0; 48 }