1. 程式人生 > >漢諾塔C語言實現(純程式碼)

漢諾塔C語言實現(純程式碼)

(本篇只為記錄程式碼,不加註解)

a、b、c三座塔,將n個從小到大(自上而下)的圓盤從a移動到c,移動期間小圓盤必須在大圓盤上面,問移動步驟。

#include<stdio.h>

int main() 
{
    void hanoi(int n,char one,char two,char three);
	int m;
	printf("請輸入盤子數:");
	scanf("%d",&m);
	printf("移動%d個盤子的步驟是:\n",m);
	hanoi(m,'A','B','C');
	getchar();
	getchar();
}
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)
{
	printf("%c->%c\n",x,y);
}

效果如下: