Hanoi Tower
#include <stdio.h>#include <stdlib.h>
void mov(int n, char x, char y, char z);
int main()
{
char x, y, z;
x = 'x';
y = 'y';
z = 'z';
printf("Please input the hanoi tower number\n");
int n;
scanf("%d", &n);
mov(n, x, y, z);
}
void mov(int n, char x, char y, char z) {
if (1 == n) {
printf("%c --> %c\n", x, z);
}
else {
mov(n - 1, x, z, y);
printf("%c --> %c\n", x, z);
mov(n - 1, y, x, z);
}
}
页:
[1]