河内塔C++(使用递归)
Tower of Hanoi C++(using recursion)
我编写了以下代码作为练习
打印目标堆栈时,输出不正确
有人能指出我哪里错了吗?
//Tower of Hanoi using Stacks!
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class Stack
{
private:
int *t;
int length, top;
public:
Stack(int len)
{
length=len;
t= new int[len];
top=-1;
}
~Stack()
{
delete []t;
}
void push(int d)
{
top++;
t[top]=d;
}
int pop()
{
top--;
return t[top+1];
}
void printstack()
{
int cur=top;
while(cur>-1)
{
cout<<t[cur]<<endl;
cur--;
}
}
};
void MoveTowerofHanoi(int disk, Stack *source, Stack *temp, Stack *destination)
{
if (disk==0)
{
destination->push(source->pop());
}
else
{
MoveTowerofHanoi(disk-1,source,temp,destination);
destination->push(source->pop());
MoveTowerofHanoi(disk-1,temp,destination,source);
}
}
void main()
{
clrscr();
int disks;
cout<<"Enter the number of disks!"<<endl;
cin>>disks;
Stack* source=new Stack(disks);
for(int i=0; i<disks; i++) {
source->push(disks-i);
}
cout<<"Printing Source!"<<endl;
source->printstack();
Stack* temp=new Stack(disks);
Stack* destination=new Stack(disks);
MoveTowerofHanoi(disks,source,temp,destination);
cout<<"Printing Destination!"<<endl;
destination->printstack();
getch();
}
这是我得到的输出:
Enter the no. of disks!
3
Printing Source!
1
2
3
Printing Destination!
-4
编辑后,代码如下所示:
void MoveTowerofHanoi(int disk, Stack *source, Stack *destination, Stack *temp)
{
if (disk==1)
{
destination->push(source->pop());
}
else
{
MoveTowerofHanoi(disk-1,source,temp,destination);
destination->push(source->pop());
MoveTowerofHanoi(disk-1,temp,destination,source);
}
}
第一个错误是:
void MoveTowerofHanoi(int disk, Stack *source, Stack *temp, Stack *destination)
第二个是:
if (disk==0)
非常感谢大家的帮助!
对堆栈类进行的更改:
void push(int d)
{
if(top<length-1)
{
top++;
t[top]=d;
}
}
int pop()
{
if(top>-1)
{
top--;
return t[top+1];
}
}
这是有效的:
//Tower of Hanoi using Stacks!
#include<iostream>
//#include<conio.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
using namespace std;
void print_elem(int elem)
{
cout << elem << endl;
}
class Stack{
public:
void push(int d){t.push_back(d);}
int pop()
{
int d=t.back();
t.pop_back();
return d;
}
void printstack()
{
for_each(t.rbegin(),t.rend(),print_elem);
}
private:
vector<int> t;
};
void MoveTowerofHanoi(int disk, Stack *source, Stack *temp, Stack *destination)
{
if (disk==1)
{
destination->push(source->pop());
}
else
{
MoveTowerofHanoi(disk-1,source,destination,temp);
destination->push(source->pop());
MoveTowerofHanoi(disk-1,temp,source,destination);
}
}
int main()
{
int disks;
cout<<"Enter the number of disks!"<<endl;
cin>>disks;
Stack* source = new Stack();
for(int i=disks; i>0; --i) {
source->push(i);
}
cout<<"Printing Source!"<<endl;
source->printstack();
Stack* temp = new Stack();
Stack* destination = new Stack();
MoveTowerofHanoi(disks,source,temp,destination);
cout<<"Printing Destination!"<<endl;
destination->printstack();
delete source;
delete temp;
delete destination;
}
相关文章:
- 通过递归进行因子分解
- 递归函数计算序列中的平方和(并输出过程)
- 使用递归的数组的最小值.这是怎么回事
- 递归列出所有目录中的C++与Python与Ruby的性能
- 如何使用内联递归?
- 递归函数的内联
- 这是河内算法的递归塔是一种不知情的搜索
- 使用递归模板函数是否会引入函数调用开销,或者编译器是否大部分时间都内联它(下面的示例)?
- 河内塔 - 如何不跳过每次递归的钉子
- 有 64 个圆盘的河内塔需要多长时间才能递归求解?C++
- 递归函数内的计数
- 以嵌套或递归方式(即在处理程序内)调用 asio io_service poll() 或 poll_one() 是否有效
- 如何在 c++ 中将内置堆栈作为对递归函数的引用传递
- 河内塔C++(使用递归)
- 用递归c++求解10个板的河内塔
- 内联函数不能包含递归、go to循环等
- 用内部类函数递归地添加数组数
- 强制内联递归函数
- 递归的河内塔,访问冲突/分段错误,在dev c++编译器上
- 内联无限递归