C++ 生成所有可能组合的列表,并从列表中随机选择一个组合
我在教程点在线找到了这段代码。链接 我试着考虑如何修改它,以便它可以从生成的列表中随机地生成一个组合,但我还没有弄明白C++ 生成所有可能组合的列表,并从列表中随机选择一个组合,c++,C++,我在教程点在线找到了这段代码。链接 我试着考虑如何修改它,以便它可以从生成的列表中随机地生成一个组合,但我还没有弄明白 #include<iostream> using namespace std; void Combi(char a[], int reqLen, int s, int currLen, bool check[], int l) { if(currLen > reqLen) return; else if (currLen == r
#include<iostream>
using namespace std;
void Combi(char a[], int reqLen, int s, int currLen, bool check[], int l)
{
if(currLen > reqLen)
return;
else if (currLen == reqLen) {
cout<<"\t";
for (int i = 0; i < l; i++) {
if (check[i] == true) {
cout<<a[i]<<" ";
}
}
cout<<"\n";
return;
}
if (s == l) {
return;
}
check[s] = true;
Combi(a, reqLen, s + 1, currLen + 1, check, l);
check[s] = false;
Combi(a, reqLen, s + 1, currLen, check, l);
}
int main() {
int i,n;
bool check[n];
cout<<"Enter the number of element array have: ";
cin>>n;
char a[n];
cout<<"\n";
for(i = 0; i < n; i++) {
cout<<"Enter "<<i+1<<" element: ";
cin>>a[i];
check[i] = false;
}
for(i = 1; i <= n; i++) {
cout<<"\nThe all possible combination of length "<<i<<" for the given array set:\n";
Combi(a, i, 0, 0, check, n);
}
return 0;
}
我不是C++专家,但我认为你应该从ArrayLenght到ArayLyHT中添加一个随机数,至少这个工作在用C++ +/P>编写的Python中。
我希望我正确理解了你的问题为什么这似乎是一个数学作业。我们通常不会为你做作业。如果你随机获取一个解决方案,为什么不选择4个随机数并跳过昂贵的组合步骤?这5个事件是如何涉及的?