标签:style blog color os io for 问题 div
#include<iostream> #include<vector> using namespace std; vector<int>v; int w[5]={3,3,3,3,3}; int p[5]={5,5,5,5,5}; void result(int**c,int i,int j); int main() { int **c=new int*[6]; for(int i=0;i<6;i++) { *(c+i)=new int[11]; } for(int i=0;i<6;i++) { for(int j=0;j<11;j++) { if(i == 0 || j == 0) { c[i][j]=0; } else { if(w[i-1]>j) { c[i][j]=c[i-1][j]; } else { c[i][j]=max(c[i-1][j-w[i-1]]+p[i-1],c[i-1][j]); } } } } cout<<c[5][10]<<endl; result(c,5,10); for(int i=0;i<6;i++) { delete[]*(c+i); *(c+i)=NULL; } delete[]c; c=NULL; return 0; } void result(int**c,int i,int j) { if(i==0 || j==0) { for(int i=0;i<v.size();i++) { cout<<v[i]<<" "; } cout<<endl; return; } if(w[i-1]>j) { result(c,i-1,j); } else { if(c[i-1][j-w[i-1]]+p[i-1] > c[i-1][j]) { v.push_back(i); result(c,i-1,j-w[i-1]); } else if(c[i-1][j-w[i-1]]+p[i-1] == c[i-1][j]) { vector<int>temp(v); v.push_back(i); result(c,i-1,j-w[i-1]); v.swap(temp); result(c,i-1,j); } else { result(c,i-1,j); } } }
标签:style blog color os io for 问题 div
原文地址:http://www.cnblogs.com/johnsblog/p/3887546.html