汉若塔问题(递归)

 1 #include <iostream>
 2 #include <stdio.h>
 3 using namespace std;
 4 
 5 static int step = 0;
 6 void move ( char sour, char dest )
 7 {
 8     printf ( "move from %c to %c \n", sour, dest );
 9 }
10 
11 void hanoi ( int n, char sour, char temp, char dest )  
12 {
13     if ( n == 1 )
14     {
15         move ( sour, dest );
16         ++step;
17     }
18     else
19     {
20         hanoi ( n-1, sour, dest, temp );
21         move ( sour,dest );
22         ++step;
23         hanoi ( n-1, temp, sour, dest );
24     }
25 }
26 int main ( int argc, char **argv )
27 {
28     int n = 4;
29     hanoi ( n, 'A', 'B', 'C' );
30     printf ( "Total steps is %d\n", step );
31     return 0;
32 }
View Code

 

 

转载于:http://www.cnblogs.com/DanielZheng/archive/2011/08/20/2146453.html

posted on 2016-02-12 23:10  青春的梦想付诸行动  阅读(172)  评论(0编辑  收藏  举报

导航