⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hannoi.cpp

📁 本程序通过用递归调用的方法实现了汉诺塔问题,这是一个通用的程序,通过输入的层数结果输入到一个out.txt的文件中
💻 CPP
字号:
#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -