调用自己定义的函数完成汉诺塔
时间: 2023-09-28 09:13:13 浏览: 32
好的,我可以帮你写一个汉诺塔的函数,如下所示:
```python
def hanoi(n, a, b, c):
if n == 1:
print("将第%d个盘子从%s移动到%s" % (n, a, c))
else:
hanoi(n-1, a, c, b)
print("将第%d个盘子从%s移动到%s" % (n, a, c))
hanoi(n-1, b, a, c)
# 示例
hanoi(3, 'A', 'B', 'C')
```
其中,`n` 表示盘子的数量,`a`、`b`、`c` 分别表示三个柱子的名称。在这个函数中,我们首先判断是否只有一个盘子,如果是,直接将其从 `a` 柱子移动到 `c` 柱子;否则,我们先将 `n-1` 个盘子从 `a` 柱子移动到 `b` 柱子,再将第 `n` 个盘子从 `a` 柱子移动到 `c` 柱子,最后将 `n-1` 个盘子从 `b` 柱子移动到 `c` 柱子。
相关问题
C语言栈完成汉诺塔算法
好的,以下是C语言栈完成汉诺塔算法的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef struct {
int elem[MAXSIZE];
int top;
} Stack;
void InitStack(Stack *S) {
S->top = -1;
}
int StackEmpty(Stack *S) {
if (S->top == -1) {
return 1;
} else {
return 0;
}
}
int Push(Stack *S, int e) {
if (S->top == MAXSIZE - 1) {
return 0;
} else {
S->top++;
S->elem[S->top] = e;
return 1;
}
}
int Pop(Stack *S, int *e) {
if (StackEmpty(S)) {
return 0;
} else {
*e = S->elem[S->top];
S->top--;
return 1;
}
}
void move(char x, char y) {
printf("%c -> %c\n", x, y);
}
void Hanoi(int n, char x, char y, char z) {
Stack S;
InitStack(&S);
int e;
Push(&S, n);
while (!StackEmpty(&S)) {
while (Pop(&S, &e) && e > 1) {
Push(&S, e - 1);
Push(&S, 0);
Push(&S, e - 1);
}
if (e == 1) {
move(x, z);
}
while (Pop(&S, &e) && e == 0) {
move(x, y);
Push(&S, 0);
}
if (e > 1) {
Push(&S, e - 1);
move(x, z);
Push(&S, 0);
Push(&S, e - 1);
}
}
}
int main() {
int n;
printf("请输入盘子的个数:");
scanf("%d", &n);
Hanoi(n, 'A', 'B', 'C');
return 0;
}
```
在这个示例代码中,我们使用了栈来模拟汉诺塔算法中的递归过程。具体来说,我们定义了一个栈结构体,包括一个数组和一个栈顶指针。我们使用栈来记录每个递归函数调用时的参数,以及返回时需要执行的操作。其中,当栈顶元素为0时,表示需要将上一个元素从x移动到y;当栈顶元素为正整数时,表示需要将上一个元素从x移动到z,并将下一个元素从x移动到y。
在主函数中,我们首先读取用户输入的盘子个数,然后调用Hanoi函数进行汉诺塔求解。在Hanoi函数中,我们首先将汉诺塔问题的初始状态压入栈中,然后进入循环,直到栈为空。在循环中,我们首先将栈顶元素不断拆分,直到其变为1,表示可以直接将盘子从x移动到z。然后,我们将栈中的0全部弹出,将上一个元素从x移动到y,并将0压入栈中。最后,如果栈顶元素大于1,则将其拆分,并将上一个元素从x移动到z。最终,当栈为空时,汉诺塔问题的求解完成。
希望这个示例代码能够帮助你理解使用栈完成汉诺塔算法的思路。
汉诺塔程序设计c++函数
汉诺塔程序设计的C函数可以有多种实现方式。以下是其中一种可能的实现方式:
```c
#include <iostream>
using namespace std;
void move(int n, char source, char destination) {
cout << "Move disk " << n << " from " << source << " to " << destination << endl;
}
void hanoi(int n, char source, char auxiliary, char destination) {
if (n == 1) {
move(n, source, destination);
} else {
hanoi(n - 1, source, destination, auxiliary);
move(n, source, destination);
hanoi(n - 1, auxiliary, source, destination);
}
}
int main() {
int n;
cout << "Enter the number of disks: ";
cin >> n;
hanoi(n, 'A', 'B', 'C');
return 0;
}
```
在这个程序中,首先定义了一个`move`函数,用于输出移动的步骤。然后,定义了`hanoi`函数,使用递归的方式实现汉诺塔的移动步骤。最后,在`main`函数中获取用户输入的圆盘数,调用`hanoi`函数进行汉诺塔的移动。
请注意,这只是一种可能的实现方式,还可以根据需求进行修改和优化。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [c++实现汉诺塔(图文并茂)](https://blog.csdn.net/lijingheng66/article/details/125769535)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [汉诺塔(c++)](https://blog.csdn.net/m0_73820126/article/details/127589006)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]