#include <iostream> using namespace std; void move_disk(char src,char dst) { cout<<src<< "=========>"<<dst<<endl; } void towers(int n,char src, char mid,char dst) { if(n==1) { move_disk(src,dst); return ; } towers(n-1,src,dst,mid); move_disk(src,dst); towers(n-1,mid,src,dst); } int main() { int disks; cout<< "Number of disks:"; cin>>disks; towers(disks, 'A','B' ,'C'); return 0; }
//物?品??重?量??S,??共2n件t物?品??,??从???其?中D选?出?若??干??件t放??在??背?3包???里??,??使?1得??重?量??之?和??为as #include<iostream> using namespace std; int W[10]; bool Knap(int s,int n) { if(s==0) return 1; if(s<0 || s>0&& n<1) return 0; if(Knap(s-W[n],n-1) == 1) { cout<<W[n]<< " "; return 1; } return Knap(s,n-1); } int main() { W[0]=0; W[1]=1;W[2]=4;W[3]=8;W[4]=16;W[5]=32; int s = 5; int n = 4; Knap(5,4); return 0; }