标题:排列组合算法
只看楼主
BlueGuy
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:29
帖 子:4476
专家分:4055
注 册:2009-4-18
结帖率:94.72%
 问题点数:0 回复次数:0 
排列组合算法
排列算法
程序代码:
#include <stdio.h>
#include <string.h>

#define NUM 4
#define MAX_DEPTH 2

int mark[NUM+1] = {0};
int printList[NUM+1] = {0};

void dfs(int depth);

int main(void)
{
    dfs(1);
    getchar();

    return 0;
}

void dfs(int depth) 
{ 
    char s[40] = {0};
    int i, j, k;

    for (i = 1; i <= NUM; i++)
    {
        if (!mark[i])
        { 
            mark[i] = 1;
            printList[depth] = i;

            if (depth < MAX_DEPTH)
            {
                dfs(depth+1);
            }

            else
            {
                for (j = 1; j <= MAX_DEPTH; j++)
                { 
                    printf("%d ", printList[j]);
                }

                printf("\n");
            }

            mark[i] = 0; 
        } 
    }
}



组合算法
程序代码:
#include <stdio.h>
#include <string.h>

#define N 3
#define M 5

int combination[N+1] = {0};

void recursion(int m, int depth);

int main(void)
{
    recursion(1, 1);
    getchar();
    return 0;
} 

void recursion(int m, int depth)
{
    char s[40] = {0};
    int i, j, k;

    for (i = m; i <= M; i++)
    {
        combination[depth] = i;

        if (depth == N)
        {
            for (j = 1; j <= N; j++)
            {
                printf("%d ", combination[j]);
            }

            printf("\n");
        }
        else
        {
            recursion(i+1, depth+1);
        }
    }
}

搜索更多相关主题的帖子: 算法 int void for include 
2023-01-15 14:34



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-511116-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.050290 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved