河內塔問題 #include<stdio.h> #include<stdlib.h> int fun_a(int) void fun_b(int,int,int,int) int main(void) { int n int option printf("題目二:河內塔問題\n") printf("請輸入要搬移的圓盤數目\n") scanf("%d",&n) printf("最少搬移的次數為%d次\n",fun_a(n)) printf("是否顯示移動過程? 是請輸入1,否則輸入0\n") scanf("%d",&option) if(option==1) { fun_b(n,1,2,3) } system("pause") return 0 } int fun_a(int n) { int sum1=2,sum2=0,i for(i=n i>1 i--) { sum1=sum1*2 } sum2=sum1-1 return sum2 } void fun_b(int n,int left,int mid,int right) { if(n==1) printf("把第%d個盤子從第%d座塔移動到第%d座塔\n",n,left,right) else { fun_b(n-1,left,right,mid) printf("把第%d個盤子從第%d座塔移動到第%d座塔\n",n,left,right) fun_b(n-1,mid,left,right) } }