您的位置:首页 > 其它

求一个长度为n的数组中长度为m的所有排列组合。

2018-03-27 13:22 615 查看
//求一个长度为n的数组中长度为m的所有排列组合。
#include <iostream>
#include <stack>
using namespace std;
stack<string> st;
void Grial(string a[], int m,int n,int length)
{
    if (st.size() == length)
    {
        stack<string> 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()
{
    string a[] = {"A","B","C","D","E"};
    Grial(a,0, sizeof(a) / sizeof(int),2);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐