文章
16
粉丝
134
获赞
0
访问
11.1k
//汉诺塔输出每一个步骤,五个一换行
#include<bits/stdc++.h>
using namespace std;
int step;//记录移动步数
void Hanoi(int n, char a, char b, char c) {
if(n == 1) {
if((step + 1) % 5 == 0)
cout << a << "-->" << c << endl;
else
cout << a << "-->" << c << " ";
step++;
return;
}
Hanoi(n - 1, a, c, b);
Hanoi(1, a, b, c);
Hanoi(n - 1, b, a, c);
}
int main() {
int n;
while(cin >> n) {
if(n == 0)
break;
step = 0;
Hanoi(n, 'A', 'B', 'C');
cout << endl;
}
return 0;
}
登录后发布评论
暂无评论,来抢沙发