Hanoi(漢諾塔)問題

#include<stdio.h>
int main()
{
    //函數聲明
    void hanoi(int n,char one,char two,char three);
    int m;
    printf("input the number of disks:");
    scanf("%d",&m);
    hanoi(m,‘A’,'B','C');
}
//將n個盤子從one,移動到three,藉助two.
void hanoi(int n,char one,char two,char three)
{
    void move(char x,char y);
    if(n==1)
        move(one,there);
    else
    {
        hanoi(n-1,one,three,two);
        move(one,three);
        hanoi(n-1,two,one,three);
    }       
}
void move(char x,char y)
{
    printf("%c->%c\n",x,y);
}
發佈了21 篇原創文章 · 獲贊 9 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章