pta L2-003 月饼
作者:互联网
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805071789801472
很水的一道题,典型的分割金币--部分背包问题,上板子就可以了;
需要注意的是价格和重量用double,不然第二个测试点过不了
Talk is cheap. Show me the code.
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct node 4 { 5 double value;//用double 6 double weight;//用double,否则会有一个测试点过不了 7 double cp; 8 }a[1110]; 9 int n,v; 10 double sum; 11 bool cmp(node x,node y) 12 { 13 return (x.cp>y.cp); 14 } 15 int main() 16 { 17 cin>>n>>v; 18 for(register int i=1;i<=n;i++) 19 { 20 cin>>a[i].weight; 21 } 22 for(register int i=1;i<=n;i++) 23 { 24 cin>>a[i].value; 25 a[i].cp=(double)a[i].value/a[i].weight; 26 } 27 sort(a+1,a+1+n,cmp); 28 for(register int i=1;i<=n;i++) 29 { 30 if(v>=a[i].weight) 31 { 32 sum+=a[i].value; 33 v-=a[i].weight; 34 } 35 else 36 { 37 sum+=v*a[i].cp; 38 break; 39 } 40 } 41 printf("%.2f",sum); 42 return 0; 43 }
标签:weight,int,double,sum,pta,003,L2,value,cp 来源: https://www.cnblogs.com/LQS-blog/p/16168291.html