用VC++编程实现Hanoi塔问题。
谢谢各位咯!
用VC++编程实现Hanoi塔问题。
谢谢各位咯!
#include <iostream.h>
#include <stdlib.h>
using namespace std;
void move(unsigned int x, unsigned int y)
{
cout<<x<<"->"<<y<<endl;
}
void TowersOfHanoi(int n, int x, int y, int z)
{
if (n > 0)
{
TowersOfHanoi(n-1, x, z, y);
move(x,y);
TowersOfHanoi(n-1, z, y, x);
}
}
int main()
{
int x=0,y=1,z=2;
TowersOfHanoi(3,x,y,z);
return 0;
}
#include<iostream> using namespace std; int step=0; void move(char a,char b) { cout<<a<<"--->"<<b<<endl; step++; } void hanoi(int n,char a,char b,char c) { if(n>0) { hanoi(n-1,a,c,b); move(a,b); hanoi(n-1,c,b,a); } }
int main() { int i; cout<<"input a number \n"; cin>>i; cout<<"the solution is : \n"; hanoi(i,'a','b','c'); cout<<"the total step numbers is "<<step<<endl; return 0; }