You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
613 B

This file contains ambiguous Unicode characters!

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
cout << A << "->" << C << endl;
HN(n - 1, B, A, C); //把B上面的盘子中转为A移动到C第三步B上的盘子移动到C
return;
}
int main()
{
int n;
cout << "请输入A上有几个按顺序排好的碟子";
cin >> n;
cout << "移动顺序如下\n";
HN(n, 'A', 'B', 'C');
return 0;
}