码迷,mamicode.com
首页 > 其他好文 > 详细

汉若塔问题(递归)

时间:2016-02-12 23:21:39      阅读:485      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
 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

汉若塔问题(递归)

标签:

原文地址:http://www.cnblogs.com/WDKER/p/5187341.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!