永发信息网

怎样用VC++编程实现Hanoi塔问题?

答案:3  悬赏:10  手机版
解决时间 2021-07-26 17:05

用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; }

#include <iostream> using namespace std; void move(int n,char s, char d) { cout<<n<<","<<s<<" ̄"<<d<<endl; } void hanoi (int n,char a='a',char b='b',char c='c') { if(1==n) move(n,a,c); else { hanoi(n-1,a,c,b); move(n,a,c); hanoi(n-1,b,a,c); } } int main () { int a; cin>>a; hanoi(a); return 0; }
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
诸暨那里有白猫喷洁净 批发
为什么我每次考试总是停留在中间,我明明很努
幽呼的《炼狱》已经更新到41章了。。。
还有什么好听的 英文歌 被翻译成中文的
我梦见一个陌生女孩给我打了三次电话
关于设计灯箱
电子公文的处理,下的3dmax模型,贴图和MAX文
桌面没用的图标怎么直接拖不走非得剪切
创建省级文明乡镇标语,创建文明城市小标语
泊头到承德多远、做火车多少钱 、车次是多少
QQ空間怎样整板块图
空间的不开了?
多变的“看”
博肯人偶除了土罐那还可以在哪里获得
什么是痛彻心扉?
推荐资讯
.怎么建个文本文档呢?
大话2密保卡被人换了一个月以上,现在我还能
有里知花的《去见你》的日文歌词及中文释意。
狂战带娃娃好还是贝尔
QQ寻仙怎样看自己的升级经验
大成基金为什么没有基金进账?
粤语字怎么翻译成英语 要翻译 我不太能看得懂
电流1.5安培A等于多少伏特V求大神帮助
将纯水加热,水中会有空气气泡冒出 能用勒夏特
用英语写你春节的计划一定要小学水平一定要
the weather is a bit cold yesterday.为什么
彩妆哪个牌子性价比高,哪个牌子的瓷砖好?且
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?