|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
盲盒,非粘贴。
请分析一下我这个思路,做出更正。
- // 汉诺塔
- #include <iostream>
- #include <cstring>
- using namespace std;
- void move(int s , char a , char b , char c){
- if (s==1) return;
- cout<<a<<"--"<<b<<endl;
- move(s-1,a,c,b);
- cout<<a<<"--"<<c<<endl;
-
- }
-
- int main(){
- int n=4;
- move(n,'a','b','c'); // 4层盘,从a杆开始,以b杆为中介,移动到c杆
-
- return 0;
- }
复制代码
- // 汉诺塔
- #include <iostream>
- #include <cstring>
- using namespace std;
- void move(int s, char a, char b, char c) {
- if (s == 1) { cout << a << "--" << c << endl; return; }
- move(s - 1, a, c, b); // 把n-1的圆环从a移动到b,递归调用下一层就是把a移动到c
- cout << a << "--" << c << endl;
- move(s - 1, b, a, c); //把n-1的圆环从b移动到c
- }
- int main() {
- int n = 4;
- move(n, 'a', 'b', 'c'); // 4层盘,从a杆开始,以b杆为中介,移动到c杆
- return 0;
- }
复制代码
|
|