汉诺塔问题是一个著名的问题,初始模型如图所示。其来源据说是在约19世纪末欧洲的商店中出售一种智力玩具,在一块铜板上有三根杆,最左边的杆自上而下、由小到大顺序串着64个圆盘构成的塔,游戏的目的是将最左边A杆上的圆盘,借助最右边的C杆,全部移动到中间的B杆上,条件是一次仅能移动一个盘,且不允许大盘放在小盘的上面。 **输入格式要求:"%d" 提示信息:"Please enter the number of discs:" **输出格式要求:"\tTotal:%d\n" "%2d-(%2d):%c==>%c\n" 程序运行示例如下: Please enter the number of discs: Please enter the number of discs: 5 1-( 1):a==>b 2-( 2):a==>c 3-( 1):b==>c 4-( 3):a==>b 5-( 1):c==>a 6-( 2):c==>b 7-( 1):a==>b 8-( 4):a==>c 9-( 1):b==>c 10-( 2):b==>a 11-( 1):c==>a 12-( 3):b==>c 13-( 1):a==>b 14-( 2):a==>c 15-( 1):b==>c 16-( 5):a==>b 17-( 1):c==>a 18-( 2):c==>b 19-( 1):a==>b 20-( 3):c==>a 21-( 1):b==>c 22-( 2):b==>a 23-( 1):c==>a 24-( 4):c==>b 25-( 1):a==>b 26-( 2):a==>c 27-( 1):b==>c 28-( 3):a==>b 29-( 1):c==>a 30-( 2):c==>b 31-( 1):a==>b Total:31 1C语言代码
时间: 2023-08-15 14:07:09 浏览: 91
汉诺塔问题的解模型分析建模
```c
#include <stdio.h>
void hanoi(int n, char a, char b, char c, int* count) {
if (n == 1) {
printf("%2d-(%2d):%c==>%c\n", ++(*count), n, a, c);
} else {
hanoi(n-1, a, c, b, count);
printf("%2d-(%2d):%c==>%c\n", ++(*count), n, a, c);
hanoi(n-1, b, a, c, count);
}
}
int main() {
int n, count = 0;
printf("Please enter the number of discs:");
scanf("%d", &n);
hanoi(n, 'a', 'b', 'c', &count);
printf("\tTotal:%d\n", count);
return 0;
}
```
阅读全文