hannoi.cpp

来自「本程序通过用递归调用的方法实现了汉诺塔问题,这是一个通用的程序,通过输入的层数结」· C++ 代码 · 共 32 行

CPP
32
字号
#include<iostream>
#include<fstream>
using namespace std;

void move(int n,char x,char y);
void hannoi(int n,char a,char b,char c);
ofstream out("out.txt");
void main()
{
	int n;
	cout<<"please input the hannoi's deepth:\n";
	cin>>n;
	out<<"The multuplate process as fllows:\n";
	hannoi(n,'A','B','C');
	cout<<"the result has input into the out text!\n";

}
void move(int n,char x,char y)
{
	out<<"The "<<n<<"disk was removed from"<<x<<"to"<<y<<" : "<<x<<"-->"<<y<<endl;
}
void hannoi(int n,char a,char b,char c)
{
	if(n==1)
		move(1,a,c);
	else
	{
		hannoi(n-1,a,c,b);
		move(n,a,c);
		hannoi(n-1,b,a,c);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?