1 #include "iostream" 2 using namespace std; 3 4 float MAX(float m1,float m2){ 5 if(m1>=m2) 6 return m1; 7 else 8 return m2; 9 } 10 11 float bag_Zero_One(int n,float v,float p[],float w[]){ 12 if(n==0||v==0) 13 return 0; 14 else{ 15 float m2; 16 17 m2=bag_Zero_One(n-1,v,p,w); 18 if(v>=w[n]){ 19 float m1; 20 m1=bag_Zero_One(n-1,v-w[n],p,w)+p[n]; 21 m2=MAX(m1,m2); 22 } 23 return m2; 24 } 25 } 26 27 void main(){ 28 float p[]={0,10,3,2}; 29 float w[]={0,5,6,7}; 30 float v=12; 31 cout<<"Result:"<<bag_Zero_One(3,v,p,w)<<endl; 32 getchar(); 33 }
算法导论三剑客之 动态规划 0-1背包问题,布布扣,bubuko.com
时间: 2024-12-25 11:37:32