//递归算法
#include<iostream>
using namespace std;
void Hanoi(int n, char A, char B, char C)
{
if (n == 1) cout << A << "-->" << C;
else {
Hanoi(n - 1, A, C, B);
cout << A << "-->" << C;
Hanoi(n - 1, B, A, C);
}
}
int main() {
int n;
cin >> n;
Hanoi(n, 'A', 'B', 'C');
return 0;
}
汉诺塔(递归实现)
最新推荐文章于 2024-08-31 22:19:14 发布