文章
55
粉丝
100
获赞
12
访问
33.1k
#include <iostream>
using namespace std;
int stepCount = 0; // 计数器,用于控制换行
void hanoi(int n, char a, char b, char c) {
if (n == 1) {
cout << a << "-->" << c << " ";
stepCount++;
if (stepCount % 5 == 0) {
cout << endl;
}
} else {
hanoi(n - 1, a, c, b);
cout << a << "-->" << c << " ";
stepCount++;
if (stepCount % 5 == 0) {
cout << endl;
}
hanoi(n - 1, b, a, c);
}
}
int main() {
int n;
while(cin>>n && n!=0){
hanoi(n, 'A', 'B', 'C');
cout << endl; // 输出最后一个换行
stepCount = 0;
}
return 0;
}
登录后发布评论
暂无评论,来抢沙发