This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
#include<iostream>
using namespace std;
void HN(int n, char A, char B, char C)
{
if (n == 1)
{ //起始位置只剩下一个时,直接移动到目的地(第二步,最后一个盘子移动到C)
cout << A << "->" << C << endl;
return;
}
HN(n - 1, A, C, B); //把A上面的盘子,中转为C,移动到B (就是第一步,把盘子移动到B)
HN(n - 1, B, A, C); //把B上面的盘子,中转为A,移动到C(第三步,B上的盘子移动到C)
int main()
int n;
cout << "请输入A上有几个按顺序排好的碟子:";
cin >> n;
cout << "移动顺序如下\n";
HN(n, 'A', 'B', 'C');
return 0;