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 }

 

相关文章:

  • 2022-12-23
  • 2021-11-23
  • 2021-10-31
  • 2021-10-22
  • 2022-12-23
  • 2021-09-29
  • 2021-09-03
  • 2021-12-03
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-27
  • 2022-01-20
  • 2021-06-17
  • 2022-12-23
  • 2021-12-07
相关资源
相似解决方案