C++:数组排列组合的问题。

来源:互联网 发布:电商运营数据分析 视频 编辑:程序博客网 时间:2024/06/06 01:11
//求一个长度为n的数组中长度为m的所有排列组合。#include <iostream>#include <stack>using namespace std;stack<int> st;void Grial(int a[], int m,int n, int length){    if (st.size() == 3)    {        stack<int> temp = st;        while (temp.empty() == false)        {            cout << temp.top() << " ";            temp.pop();        }        cout << endl;        return;    }    else    {        for (int i = m; i < n; i++)        {            st.push(a[i]);            Grial(a, i+1,n, length);            st.pop();        }    }}int main(){    int a[] = {1,2,3,4,5,6,7};    Grial(a,0, sizeof(a) / sizeof(int), 3);}*///求一个数组的全排列,代码情况如下:#include <iostream>using namespace std;void Grial(int a[], int n,int m){    if (n == m)    {        for (int i = 0; i < m; i++)        {            cout << a[i] << " ";        }        cout << endl;    }    else    {        for (int i = n; i < m; i++)        {            std::swap(a[i],a[n]);            Grial(a,n+1,m);            std::swap(a[i],a[n]);        }    }}int main(){    int a[] = { 1, 2, 3, 4 };    Grial(a, 0,sizeof(a) / sizeof(int));    return 0;}
0 0
原创粉丝点击