汉诺塔

 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 }

 

你可能感兴趣的:(汉诺塔)