Sam大叔
"if you ever want something badly,let it go.if it comes back to you,then it's yours forever.if it doesn't,then it was never yours to begin with."

导航

 
 1 //
 2 //  hanno_fib.cpp
 3 //
 4 //  Created by scandy_yuan on 12-12-27.
 5 //  Copyright (c) 2012年 Sam. All rights reserved.
 6 //
 7 
 8 #include <iostream>
 9 using namespace std;
10 
11 //汉诺塔
12 void hanno(int n,char a,char b,char c)
13 {
14     //假设只有1个盘子,即为递归退出条件
15     if(n==1){
16         cout << "the disk from" << " " << a << " " << "to" << " " << c << endl;
17         return ;
18     }
19     //n-1个盘子从a柱借助c柱移到b柱
20     hanno(n-1, a, c, b);
21     //讲第n个盘子从a柱移到c柱
22     cout << "the disk from" << " "  << a  << " " << "to" << " " << c << endl;
23     //将n-1个盘子从b柱借助a柱移到c柱
24     hanno(n-1, b, a, c);
25 }
26 
27 //斐波那契数列
28 int fib(int n)
29 {
30     return (n==0)?0:(n==1||n==2)?1:fib(n-1)+fib(n-2);
31 }
32 
33 int main(int argc, const char * argv[])
34 {
35     //insert code here...
36     //测试
37     hanno(3, 'A', 'B', 'C');
38     
39     for(int i=0;i<10;i++)
40         cout << fib(i) << " ";
41     cout << endl;
42     
43     return 0;
44 }

 

posted on 2012-12-28 14:54  Sam大叔  阅读(424)  评论(0编辑  收藏  举报