Commit 74d27bc 1 parent 59ecf65 commit 74d27bc Copy full SHA for 74d27bc
File tree 1 file changed +47
-0
lines changed
Exhaustive search and generation
1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < vector>
3
+
4
+ using namespace std ;
5
+
6
+ vector<bool > mp; // marquem les permutacions
7
+
8
+ void escriure_permutacio (vector<int >& v) {
9
+ if (v.size () > 0 ) {
10
+ cout << " (" << v[0 ];
11
+ for (int i=1 ; i < v.size (); ++i) {
12
+ cout << " ," << v[i];
13
+ }
14
+ cout << " )" << endl;
15
+ }
16
+ }
17
+
18
+ void calcul_permutacions (vector<int >& v, int i) {
19
+ if (i == v.size ()) {
20
+ escriure_permutacio (v);
21
+ }
22
+ else {
23
+ for (int j=0 ; j < v.size (); ++j) {
24
+ if (not mp[j]) {
25
+ int aux = v[i];
26
+ v[i] = j + 1 ;
27
+ mp[j] = true ;
28
+ calcul_permutacions (v,i+1 );
29
+ mp[j] = false ;
30
+ v[i] = aux;
31
+ }
32
+
33
+ }
34
+ }
35
+ }
36
+
37
+ int main () {
38
+ int n;
39
+ cin >> n;
40
+ vector<int > v (n);
41
+ for (int i=0 ; i < v.size (); ++i) {
42
+ v[i] = i + 1 ;
43
+ }
44
+ mp = vector<bool > (n, false );
45
+ if (n > 1 ) calcul_permutacions (v,0 );
46
+ else escriure_permutacio (v);
47
+ }
You can’t perform that action at this time.
0 commit comments