This Blog is dedicated to Sir Johnson Lin
Learn flow chart from C language , master C with the chart!

河內塔-遞迴應用 Hanoi Tower -Recursive solution

D:\CPPcode\HanoiTower\HanoiTower.c++
 1 //++++++++++++++++++++++++++
 2  //      HanoiTower.cpp
 3  //++++++++++++++++++++++++++
 4  
 5  ///#include header
 6  #include <iostream>
 7  #include <fstream>
 8  
 9  
10  ///Using & universal
11  using namespace std;
12  int step = 0;
13  
14  
15  ///hanoi()
16  void hanoi (int n, string from, string buffer, string to)
17  {
18      
19      
20      ///last disk
21      if (n == 0) return;
22      
23      
24      ///hanoi()
25      hanoi (n - 1, from, to, buffer);
26      
27      
28      ///Display
29      step++;
30      cout << "Move disk " << n << " ---- " << from << "==> " << to <<  "   step no = " << step <<endl;
31      
32      
33      
34      ///hanoi()
35      hanoi (n - 1, buffer, from, to);
36      
37      
38      ///hanoi=
39  } //function end
40  
41  
42  ///main(i)
43  int main()
44  {
45      
46      
47      ///declare
48      int n;
49      
50      
51      ///Prompt /Keyin
52      cout <<   " Please  key in disk  number:  ";
53      cin >> n;
54      
55      
56      ///No Disk
57      if (n == 0) { cout <<" No Disk" << endl;  return 1;}
58      
59      
60      
61      ///hanoi()
62      hanoi (n, "[O] [ ] [ ]","[ ] [O] [ ]","[ ] [ ] [O]");
63      
64      
65      ///main(o)
66      return 0 ;
67  } //main_end
68  
69  

@ 下載程式碼 Download source code

@ 下載三碟流程分析 Download three disks operation flow

1 則留言: