[問題] C語言Permutation遞迴改非遞迴的問題

看板C_and_CPP (C/C++)作者 (TastychocoYo)時間13年前 (2012/11/05 02:08), 編輯推噓1(1012)
留言13則, 5人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) DevC++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 最近在練習寫C語言(新手入門) 看到Permutation這一部分 然後有自己寫出遞迴的練習 但是對於遞迴改成非遞迴一直都沒概念不太會寫 就是遞迴改成非遞迴...(抱歉很多不懂) 餵入的資料(Input): 直接定義在陣列中 char list[5] = {'1','2','3','4','5'}; 預期的正確結果(Expected Output): 假設123做排列的結果 錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版) #include "stdio.h" #include "conio.h" char list[5] = {'1' , '2' , '3' , '4' , '5'}; int n = 3; main() { int i = n; perm(0 , i); system("pause"); } perm(int index , int number) { int i; char j; if(index == number) { for(i = 0; i < number; i++) printf("%c" , list[i]); printf( "\n" ); } else { for(i = index; i < n; i++) { j = list[i]; list[i] = list[index]; list[index] = j; perm(index+1 , number); list[index] = list[i]; list[i] = j; } } } 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.24.6.215

11/05 03:19, , 1F
不用定義副函式嗎?這樣編譯會過?
11/05 03:19, 1F

11/05 03:22, , 2F
可以過@@ 就直接跑全部順序出來了!
11/05 03:22, 2F

11/05 03:34, , 3F
怎麼可能可以過, system, perm 都是找不到的 func.
11/05 03:34, 3F

11/05 03:35, , 4F
再加上 main.perm 屬 return int, 裡面都沒傳回值.
11/05 03:35, 4F

11/05 04:24, , 5F
gcc 編得過,可以執行。
11/05 04:24, 5F

11/05 04:42, , 6F
痾..真的可以過Q_Q 我這是有實際執行過的DEVC++4.9.9.2
11/05 04:42, 6F

11/05 04:42, , 7F
版本的 我下載下來就直接CODING在上面
11/05 04:42, 7F

11/05 06:25, , 8F

11/05 06:27, , 9F
用一個 stack 來記前幾層的資訊。
11/05 06:27, 9F

11/05 08:45, , 10F
gcc 3.x有一些標投檔會自動include
11/05 08:45, 10F

11/05 10:41, , 11F
謝謝LION大 不過還不太懂怎麼用STACK@@!我在想說用一般
11/05 10:41, 11F

11/05 10:42, , 12F
的ARRAY寫法來去代入
11/05 10:42, 12F

11/05 16:30, , 13F
用Visual studio2010吧!!DEV-C都快被淘汰了
11/05 16:30, 13F
文章代碼(AID): #1Gbgys6B (C_and_CPP)
文章代碼(AID): #1Gbgys6B (C_and_CPP)