汉诺塔的递推代码c语言
时间: 2023-09-17 08:13:59 浏览: 56
当然,以下是汉诺塔的递推代码(C语言):
```c
void hanoi(int n, char A, char B, char C) {
int i = 0;
for (i = 1; i < (1 << n); ++i) {
if (i & 1) printf("%c -> %c\n", A, C);
else printf("%c -> %c\n", A, B);
if (!(i & (i - 1))) {
int pow = 1;
while ((1 << pow) <= i) ++pow;
--pow;
int tmp = (i >> (pow + 1)) << (pow + 1);
while (tmp) {
if (tmp & 1) printf("%c -> %c\n", ((i >> pow) & 1) ? B : C, ((i >> pow) & 1) ? C : B);
else printf("%c -> %c\n", A, ((i >> pow) & 1) ? B : C);
--pow, tmp >>= 1;
}
}
}
}
```
希望这能帮到你!
相关问题
汉诺塔非递归c语言代码
以下是汉诺塔的非递归C语言代码:
```
#include <stdio.h>
#include <stdlib.h>
// 定义堆栈结构
struct Stack {
int capacity;
int top;
int *array;
};
// 创建堆栈
struct Stack *createStack(int capacity) {
struct Stack *stack = (struct Stack *)malloc(sizeof(struct Stack));
stack->capacity = capacity;
stack->top = -1;
stack->array = (int *)malloc(stack->capacity * sizeof(int));
return stack;
}
// 判断堆栈是否为空
int isEmpty(struct Stack *stack) {
return stack->top == -1;
}
// 判断堆栈是否已满
int isFull(struct Stack *stack) {
return stack->top == stack->capacity - 1;
}
// 入栈
void push(struct Stack *stack, int item) {
if (isFull(stack)) {
return;
}
stack->array[++stack->top] = item;
}
// 出栈
int pop(struct Stack *stack) {
if (isEmpty(stack)) {
return -1;
}
return stack->array[stack->top--];
}
// 非递归汉诺塔函数
void hanoi(int n) {
struct Stack *source = createStack(n);
struct Stack *auxiliary = createStack(n);
struct Stack *destination = createStack(n);
int i;
for (i = n; i > 0; i--) {
push(source, i);
}
int totalMoves = (1 << n) - 1;
int from, to;
if (n % 2 == 0) {
from = 2;
to = 3;
} else {
from = 1;
to = 2;
}
for (i = 1; i <= totalMoves; i++) {
if (i % 3 == 1) {
if (isEmpty(destination)) {
push(destination, pop(source));
} else if (isEmpty(source)) {
push(source, pop(destination));
} else if (source->array[source->top] > destination->array[destination->top]) {
push(destination, pop(source));
} else {
push(source, pop(destination));
}
} else if (i % 3 == 2) {
if (isEmpty(auxiliary)) {
push(auxiliary, pop(source));
} else if (isEmpty(source)) {
push(source, pop(auxiliary));
} else if (source->array[source->top] > auxiliary->array[auxiliary->top]) {
push(auxiliary, pop(source));
} else {
push(source, pop(auxiliary));
}
} else if (i % 3 == 0) {
if (isEmpty(destination)) {
push(destination, pop(auxiliary));
} else if (isEmpty(auxiliary)) {
push(auxiliary, pop(destination));
} else if (auxiliary->array[auxiliary->top] > destination->array[destination->top]) {
push(destination, pop(auxiliary));
} else {
push(auxiliary, pop(destination));
}
}
printf("Move disk %d from rod %d to rod %d\n", i, from, to);
if (from == 1 && to == 2) {
from = 1;
to = 3;
} else if (from == 1 && to == 3) {
from = 2;
to = 3;
} else {
from = 1;
to = 2;
}
}
}
// 主函数
int main() {
int n = 3;
hanoi(n);
return 0;
}
```
汉诺塔递归算法c语言
以下是C语言实现汉诺塔递归算法的代码:
```c
#include <stdio.h>
void hanoi(int n, char A, char B, char C) {
if (n == 1) {
printf("%c -> %c\n", A, C);
} else {
hanoi(n - 1, A, C, B);
printf("%c -> %c\n", A, C);
hanoi(n - 1, B, A, C);
}
}
int main() {
int n = 3; // 汉诺塔的层数
hanoi(n, 'A', 'B', 'C');
return 0;
}
```
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)