组合数的生成

采用回朔法  调高程序的效率


#include<iostream>
#include<cstring>
using namespace std;
const int N = 1e3;
//组合数
int  num = 0;
bool vis[N];
int res[N];
void solve(int n, int cur){
if (cur>n) {
for (int i = 1; i <= n; ++i)
cout << res[i] << " ";
cout << "\n";
++num; //统计个数
}
else {
for (int i = 1; i <= n; ++i)
if (!vis[i]) {
vis[i] = true;
res[cur] = i;
solve(n, cur + 1);
vis[i] = false;
}


}


}
int main(){
int n;
cin >> n;
memset(vis, false, N);
solve(n, 1);
cout << "总共有 " << num << "种情况" << endl;
}


猜你喜欢

转载自blog.csdn.net/qq_37437892/article/details/79502859