漢諾塔問題的遞歸實現

#include<iostream>
using namespace std;
int main()
{
     void hanoi(int n,char one,char two,char three);
     int num;
     cout<<"請輸入要計算的盤子數:";
     cin>>num;
     hanoi(num,'A','B','C');
}




void hanoi(int n,char one,char two,char three)
{
     void move(char x,char y);
     if (n == 1)
     {
           move(one,three);
     }
     else
     {
         hanoi(n-1,one,three,two);
         move(one,three);
         hanoi(n-1,two,one,three);
     }
}




void move(char x, char y)
{
     cout<<x<<"-->"<<y<<endl;
}
發佈了76 篇原創文章 · 獲贊 14 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章