用C語言解決(hanoi)漢諾塔問題——函數的遞歸調用

#include <stdio.h>
void main()
{
void hanoi(int n,char one,char two,char three);
int n;
printf("請輸入需要移動的盤子數:\n");
scanf("%d",&n);
hanoi(n,'A','B','C');
}


void hanoi(int n,char one,char two,char three)
{
void move(char x,char y);
if(n==1)
{
printf("%c-->%c\n",one,three);
}
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);
}


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