1 #include<iostream> 2 #include<string> 3 4 using namespace std; 5 6 void Hanoi(int n,char A,char B,char C); 7 8 void main() 9 { 10 Hanoi(5,'A','B','C'); 11 } 12 13 void Hanoi(int n,char A,char B,char C) 14 { 15 if(n==1) 16 cout<<"Move top disk from peg "<<A<<" to peg "<<C<<endl; 17 else 18 { 19 Hanoi(n-1,A,C,B); 20 cout<<"Move top disk from peg "<<A<<" to peg "<<C<<endl; 21 Hanoi(n-1,B,A,C); 22 } 23 }
本文转自夏雪冬日博客园博客,原文链接:http://www.cnblogs.com/heyonggang/archive/2013/04/28/3049376.html,如需转载请自行联系原作者