解题思路:
水题。
注意事项:
参考代码:
#include <bits/stdc++.h> using namespace std; int step = 1; void move(char x,int n,char y){ printf("%2d. Move disk %d from %c to %c\n",step,n,x,y); step++; } void haoni(int n,char x,char y,char z){ if(n==1){ move(x,1,z); return ; } else{ haoni(n-1,x,z,y); move(x,n,z); haoni(n-1,y,x,z); return; } } int main (){ int n; while(cin>>n){ step = 1; haoni(n,'X','Y','Z'); cout<<endl; } }
0.0分
1 人评分