漢諾塔C++版的遞歸實現。。。。

 

  1. /*************************************************************** 
  2.  *                                                             *   
  3.  *  作者:祝靖俊                                               *  
  4.  *  描述:漢諾塔的遞歸實現                                     * 
  5.  *  實現語言:C++                                              * 
  6.  *  運行環境:標準C++運行環境                                  *  
  7.  *                                                             * 
  8.  ***************************************************************/ 
  9.  
  10.  
  11. #include <iostream> 
  12.  
  13. using namespace std; 
  14.  
  15. void hannoi(int count, char* target, char* from, char* temp); 
  16.  
  17. void move(int count, char* target, char* from); 
  18.  
  19. int main(){ 
  20.      
  21.     hannoi(3, "target""from""temp"); 
  22.  
  23.     return 0; 
  24.  
  25. void hannoi(int count, char* target, char* from, char* temp){ 
  26.     if(count==1){ 
  27.         move(count, target, from); 
  28.     }else
  29.         hannoi(count-1, temp, from, target);    
  30.         move(count, target, from);    
  31.         hannoi(count-1, target, temp, from); 
  32.     } 
  33.  
  34. void move(int count, char* target, char* from){ 
  35.     cout << "把" << count << "從" << from << "移動到" << target << endl; 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章