2019-10-08  2024-09-15    381 字  1 分钟

枚举

指数型

 1/* ***********************************************
 2Author        : Akvicor
 3Created Time  : Wed Sep 18 21:07:30 2019
 4File Name     : 1026.cpp
 5************************************************ */
 6
 7#include <bits/stdc++.h>
 8
 9#define FAST_IO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
10
11using namespace std;
12
13vector<int> vt;
14
15int n;
16void dfs(int x){
17	//vt.push_back(x);
18
19	if(x == n+1){
20		for(int i = 0; i < vt.size(); ++i){
21			if(i != 0) cout << ' ';
22			cout << vt[i];
23		}cout << endl;
24		return;
25	}
26	
27	dfs(x+1);
28	vt.push_back(x);
29	dfs(x+1);
30	vt.pop_back();
31	
32}
33
34int main()
35{
36	cin >> n;
37	dfs(1);
38    return 0;
39}

组合

 1/* ***********************************************
 2Author        : Akvicor
 3Created Time  : Thu Sep 19 18:31:55 2019
 4File Name     : 1027.cpp
 5************************************************ */
 6
 7#include <bits/stdc++.h>
 8
 9#define FAST_IO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
10
11using namespace std;
12int n, m;
13vector<int> vt;
14void dfs(int x, int k){
15	if(k > m || k + n-x+1 < m) return;
16	if(k == m){
17		for(int i = 0; i < vt.size(); ++i){
18			cout << vt[i] << ' ';
19		}cout << endl;
20		return;
21	}
22	vt.push_back(x);
23	dfs(x+1, k+1);
24	vt.pop_back();
25	dfs(x+1, k);
26}
27
28int main()
29{
30    FAST_IO;
31	cin >> n >> m;
32	dfs(1, 0);
33    return 0;
34}

排列

 1/* ***********************************************
 2Author        : Akvicor
 3Created Time  : Thu Sep 19 19:10:29 2019
 4File Name     : a.cpp
 5************************************************ */
 6
 7#include <bits/stdc++.h>
 8
 9#define FAST_IO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
10
11using namespace std;
12
13int n;
14
15bool vis[100];
16vector<int> v;
17
18void dfs(int k){
19	if(k == n){
20		for(auto &i : v) cout << i << ' ';
21		cout << endl;
22		return;
23	}
24	for(int i = 1; i <= n; ++i){
25		if(!vis[i]) {
26			vis[i] = true;
27			v.push_back(i);
28			dfs(k+1);
29			v.pop_back();
30			vis[i] = false;
31		}
32	}
33}
34
35void nper(int n){
36	vector<int> v(n);
37	for(int i = 0; i < n; ++i) v[i] = i+1;
38	do{
39		for(int i = 0; i < v.size(); ++i){
40			cout << v[i] << ' ';
41		}cout << endl;
42	}while(next_permutation(v.begin(), v.end()));
43}
44
45int main()
46{
47    FAST_IO;
48    cin >> n;
49	dfs(0);
50    return 0;
51}

除另有声明外本博客文章均采用 知识共享 (Creative Commons) 署名 4.0 国际许可协议 进行许可转载请注明原作者与文章出处