[問題] C語言Permutation遞迴改非遞迴的問題
看板C_and_CPP (C/C++)作者blowdriver (TastychocoYo)時間13年前 (2012/11/05 02:08)推噓1(1推 0噓 12→)留言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
11/05 03:34, 3F
→
11/05 03:35, , 4F
11/05 03:35, 4F
→
11/05 04:24, , 5F
11/05 04:24, 5F
→
11/05 04:42, , 6F
11/05 04:42, 6F
→
11/05 04:42, , 7F
11/05 04:42, 7F
→
11/05 06:25, , 8F
11/05 06:25, 8F
→
11/05 06:27, , 9F
11/05 06:27, 9F
推
11/05 08:45, , 10F
11/05 08:45, 10F
→
11/05 10:41, , 11F
11/05 10:41, 11F
→
11/05 10:42, , 12F
11/05 10:42, 12F
→
11/05 16:30, , 13F
11/05 16:30, 13F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章