请用C++代码实现汉诺塔问题
时间: 2023-04-07 15:02:42 浏览: 69
Sure, here is the C code to solve the Tower of Hanoi problem:
```
#include <stdio.h>
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)
{
if (n == 1)
{
printf("Move disk 1 from rod %c to rod %c\n", from_rod, to_rod);
return;
}
towerOfHanoi(n-1, from_rod, aux_rod, to_rod);
printf("Move disk %d from rod %c to rod %c\n", n, from_rod, to_rod);
towerOfHanoi(n-1, aux_rod, to_rod, from_rod);
}
int main()
{
int n = 3; // number of disks
towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are the names of rods
return 0;
}
```
This code will solve the Tower of Hanoi problem for 3 disks. You can change the value of `n` to solve the problem for a different number of disks.