Hanoi

 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 }

 

posted @ 2020-02-28 14:38  张乐珊  阅读(387)  评论(0)    收藏  举报