编程语言
首页 > 编程语言> > 算法训练 Hanoi问题

算法训练 Hanoi问题

作者:互联网

code

#include<iostream>
#include<algorithm>
using namespace std;
int cnt = 0 ;
void fun(int n, int m, char a, char b, char c) {
	if (n <= m) {
		cnt++;
	} else {
		fun(n - m, m, a, c, b);
		cnt++;
		cout<<a<<b<<c<<'\n';
		fun(n - m, m, b, a, c);
	}
}
int main() {
	ios::sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
	fun(n, m, 'X', 'Y', 'Z');
	cout << cnt << '\n';
	return 0;
}

标签:std,cnt,训练,int,Hanoi,char,算法,fun,include
来源: https://www.cnblogs.com/jeseesmith/p/15860073.html