汉洛塔VC++代码
- 打开VC
- 点击文件–>新建–>工程–>Win32 Control Application–>工程名填test1–>确定–>一个空工程–>完成
- 点击文件–>新建–>C++ Source File–>命名填hanoi
- 输入如下代码:
#include <iostream>
#include <stdio.h>
using namespace std;
static int step = 0;
void move (char sour,char dest)
{
printf("move from %c to %c \n",sour,dest);
}
void hanoi(int n,char sour,char temp,char dest)
{
if(n == 1)
{
move(sour,dest);
++step;
}
else
{
hanoi(n-1,sour,dest,temp);
move(sour,dest);
++step;
hanoi(n-1,temp,sour,dest);
}
}
int main(int argc,char **argv)
{
int n = 4;
hanoi(n,'A','B','C');
printf("Total steps is %d\n",step);
return 0;
}
5.点击编译–>运行–>运行结果如下: