//求一个长度为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);
}
评论区