汉诺塔(递归)

来源:互联网 发布:匡恩网络太有钱了 编辑:程序博客网 时间:2024/06/11 19:06
<pre name="code" class="cpp">#include <iostream>using namespace std;/*
method:每次处理两个盘子,A->B,A->C.B->C
*/void hanoi(int n, char A, char B, char C) { if(n == 1) { cout<<"move sheet "<<n<<" from "<<A<<" to "<<C<<endl;} else { hanoi(n-1, A, C, B); cout<<"move sheet "<<n<<" from "<<A<<" to "<<C<<endl;hanoi(n-1, B, A, C); } } int main() {int n; cout<<"please input the number of sheet:"<<endl; cin>>n;hanoi(n, 'A', 'B', 'C'); return 0; }


                                             
0 0
原创粉丝点击