分列和組合算法的完成辦法_C說話經典案例。本站提示廣大學習愛好者:(分列和組合算法的完成辦法_C說話經典案例)文章只能為提供參考,不一定能成為您想要的結果。以下是分列和組合算法的完成辦法_C說話經典案例正文
分列和組合算法是考核遞歸的罕見算法,這兩種算法能用遞歸簡練地完成。
自己在經由屢次探索和思慮以後,總結以下,以供參考。
法式代碼以下:
#include <stdio.h>
#include <stdlib.h>
char array[] = "abcd";
#define N 4
#define M 3
int queue[N] = {0};
int top = 0;
int flag[N] = {0};
void perm(int s, int n)
{
int i;
if (s > n)
{
return;
}
if (s == n)
{
for (i = 0; i < n; i++)
{
printf("%c", queue[i]);
}
printf("\t");
return ;
}
for (i = 0; i < n; i++)
{
if (flag[i] == 0)
{
flag[i] = 1;
queue[s] = array[i];
perm(s+1, n);
flag[i] = 0;
}
}
}
void comb(int s, int n, int m)
{
int i;
if (s > n)
return ;
if (top == m)
{
for (i = 0; i < m; i++)
{
printf("%c", queue[i]);
}
printf("\t");
return ;
}
queue[top++] = array[s];
comb(s+1, n, m);
top--;
comb(s+1, n, m);
}
int main()
{
printf("\nperm():\n");
perm(0, N);
printf("\ncombination():\n");
comb(0, N, M);
printf("\n");
return 0;
}
運轉成果:
perm(): abcd abdc acbd acdb adbc adcb bacd badc bcad bcda bdac bdca cabd cadb cbad cbda cdab cdba dabc dacb dbac dbca dcab dcba combination(): abc abd acd bcd
以上就是小編為年夜家帶來的分列和組合算法的完成辦法_C說話經典案例的全體內容了,願望對年夜家有所贊助,多多支撐~