排列与组合

#include <iostream>

using namespace std;


void perm(int a[], int n, int m, int out[], int k, int used[])
{
    if(k == m)
    {
        for(int i = 0; i < m; i++)
        {
            cout << out[i] << " ";
        }
        cout << endl;
    }
    for(int i = 0; i < n; i++)
    {
        if(used[i] == 0)
        {
            used[i] = 1;
            out[k] = a[i];
            perm(a, n, m, out, k+1, used);
            used[i] = 0;
        }
    }
}

void combine(int a[], int n, int m, int out[], int k, int used[], int startpos)
{
    if(k == m)
    {
        for(int i = 0; i < m; i++)
        {
            cout << out[i] << " ";
        }
        cout << endl;
    }

    for(int i = startpos; i <= startpos+(n-m); i++)  //
    {
        if(used[i] == 0)
        {
            used[i] = 1;
            out[k] = a[i];
            combine(a, n, m, out, k+1, used, startpos+1);
            used[i] = 0;
        }
    }
}

int main()
{
    int a[] = {1,2,3,4};
    int used[5] = {0};
    int out[5] = {0};

    //perm(a, sizeof(a)/sizeof(int), 3, out, 0, used);
    combine(a, sizeof(a)/sizeof(int), 3, out, 0, used, 0);
    return 0;
}


阅读更多
文章标签: include
个人分类: c++ 其他
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭