[問題] 和內塔公式

看板C_and_CPP (C/C++)作者 (呈呈)時間14年前 (2011/10/24 10:55), 編輯推噓1(102)
留言3則, 2人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) c++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 餵入的資料(Input):s #include <stdio.h> #include <stdlib.h> void move(int n,char A,char B,char C); int times=0; //全域變數 int main() { int n; printf("請輸入A柱中的環個數: "); scanf("%d", &n); move(n,'A','B','C'); printf("共需%d個步驟\n", times); system("PAUSE"); return 0; } void move(int n,char A,char B,char C) { if(n==1) { printf("ring %d : %c=>%c\n",n,A,C); times++; } else { move(n-1,A,C,B); printf("ring %d : %c=>%c\n",n,A,C); times++; move(n-1,B,A,C); } } 預期的正確結果(Expected Output): 錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版) 補充說明(Supplement): else { move(n-1,A,C,B); printf("ring %d : %c=>%c\n",n,A,C); times++; move(n-1,B,A,C); } } move(n-1,A,C,B); 我想請問這邊的A為甚麼會是把n-1個從A移動到B,他程式是怎麼操作的 另外這邊的A是指move(n,'A','B','C'); 這邊的A嗎? 還是指這邊的A void move(int n,char A,char B,char C) 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.120.25.238

10/24 10:59, , 1F
把n-1個環從A移動(暫放)到B之後,你才能把最後一個
10/24 10:59, 1F

10/24 11:00, , 2F
環從 A 搬到 C,不是嗎?另外:後者,A 是形式參數。
10/24 11:00, 2F

10/24 18:11, , 3F
/河內塔 前人有詳細說明
10/24 18:11, 3F
文章代碼(AID): #1EfDEi6T (C_and_CPP)
文章代碼(AID): #1EfDEi6T (C_and_CPP)