求解:面向对象思想解汉诺塔问题?

善水999 2011-06-02 10:22:53
能不能用面向对象编程思想求解汉诺塔问题,怎么做?
...全文
210 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
机智的呆呆 2011-06-02
  • 打赏
  • 举报
回复
面向对象思想不能滥用的~~~
试想用面向对象思想用数个设计模式来输出一个hello world能有多大意义?
longbaoer_1215 2011-06-02
  • 打赏
  • 举报
回复

please input you number of the disk on the peg A:1
Move top disk from A peg to C peg
请按任意键继续. . .

please input you number of the disk on the peg A:2
Move top disk from A peg to B peg
Move top disk from A peg to C peg
Move top disk from B peg to C peg
请按任意键继续. . .

please input you number of the disk on the peg A:3
Move top disk from A peg to C peg
Move top disk from A peg to B peg
Move top disk from C peg to B peg
Move top disk from A peg to C peg
Move top disk from B peg to A peg
Move top disk from B peg to C peg
Move top disk from A peg to C peg
请按任意键继续. . .

please input you number of the disk on the peg A:4
Move top disk from A peg to B peg
Move top disk from A peg to C peg
Move top disk from B peg to C peg
Move top disk from A peg to B peg
Move top disk from C peg to A peg
Move top disk from C peg to B peg
Move top disk from A peg to B peg
Move top disk from A peg to C peg
Move top disk from B peg to C peg
Move top disk from B peg to A peg
Move top disk from C peg to A peg
Move top disk from B peg to C peg
Move top disk from A peg to B peg
Move top disk from A peg to C peg
Move top disk from B peg to C peg
请按任意键继续. . .


longbaoer_1215 2011-06-02
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
using namespace std;
void Hanoi(int n,string & A, string & B,string & C);//move the disks from A to C with the help of B
void main(){
int n;
string a("A peg");//A 柱
string b("B peg");//B 柱
string c("C peg");//c 柱
cout<<"please input you number of the disk on the peg A:";
cin>>n;
Hanoi(n,a,b,c);
}

void Hanoi(int n,string & A, string & B,string & C){
if(n==1) cout<<"Move top disk from "<<A<<" to "<<C<<endl;
else {
Hanoi(n-1,A,C,B);//move the n-1 disks from A to B with the help of C
cout<<"Move top disk from "<<A<<" to "<<C<<endl;
Hanoi(n-1,B,A,C);//move the n-1 disks from B to C with the help of A
}
}
就想叫yoko 2011-06-02
  • 打赏
  • 举报
回复
google C++ 汉诺塔
一大把
Jim_King_2000 2011-06-02
  • 打赏
  • 举报
回复
汉诺塔是经典的递归问题,用不着面向对象。

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧