首页 > 其他 > 详细

Hanoi

时间:2020-02-28 14:52:57      阅读:59      评论:0      收藏:0      [点我收藏+]
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int hanoi(int n, char a, char c, char b);
 5 
 6 int main()
 7 {
 8     int numdisk;
 9     printf("The number of disks you want to move is: ");
10     scanf("%d", &numdisk);
11 
12     int times;
13     times = hanoi(numdisk, a, c, b);
14     printf("You need to move the disks %d times", times);
15 
16     return 0;
17 }
18 
19 int hanoi(int n, char a, char c, char b)
20 {
21     static int times = 0;
22     if(n == 1)
23         printf("move %c to %c \n", a, c);
24     else
25     {
26         hanoi(n-1, a, b, c);
27         printf("move %c to %c \n", a, b);
28         hanoi(n-1, b, c, a);
29     }
30     times ++;
31     return times;
32 
33 }

 

Hanoi

原文:https://www.cnblogs.com/zhangleshan/p/12377117.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!