注册 登录
编程论坛 C++教室

求C++做汉诺塔问题

小木郎一世 发布于 2010-10-12 11:20, 458 次点击
求C++做汉诺塔问题
3 回复
#2
小木郎一世2010-10-12 11:20
求C++做汉诺塔问题
#3
hahayezhe2010-10-12 11:47
google
#4
m21wo2010-10-12 18:57
就是用递归啊!
程序代码:
#include <iostream>
using namespace std;
void move(char x, char y )
{
    cout<<x<<"->"<<y<<endl;
}
void hanio(int n,char one ,char two,char three)
{
    if(n==1)
        move(one,three);
    else
    {
        hanio(n-1,one,three,two);
        move(one,three);
        hanio(n-1,two,one,three);
    }
}
int main()
{
    int m;
    cout<<"please input the number of the hanio:"<<endl;
    cin>>m;
    cout<<"The steps of moving "<<m<<"   disks"<<endl;
    hanio(m,'A','B','C');
}
1