-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathk_permutacje.cpp
More file actions
84 lines (66 loc) · 1.4 KB
/
Copy pathk_permutacje.cpp
File metadata and controls
84 lines (66 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
//#define DEBUG
#ifdef DEBUG
ostream& debug = cout;
#else
ostringstream nullStream;
ostream& debug = nullStream;
#endif
void k_permutacji(const int n, char* S, const int k);
void permutuj(int left, int right, char*S,const int k,int counter);
char* zamien(int left, int i,char*S);
int silnia(int n)
{
if (n <= 0) return 1;
return n * silnia(n - 1);
}
int main(int argc,char*argv[])
{
const int n = atoi(argv[1]); //N_liczb
char* S = argv[2];
const int k = atoi(argv[3]); //K-permutacje
const int ile_permutacji = silnia(n) / silnia(n-k);
cout << "ILE PERMUTACJI: " << ile_permutacji << '\n';
k_permutacji(n, S, k);
return 0;
}
void k_permutacji(const int n, char* S, const int k)
{
permutuj(0, n - 1,S,k,0);
}
void permutuj(int left, int right, char* S, const int k,int counter)
{
if (counter == k)
{
debug << "COUNTER: " << counter << '\n';
debug << "WYNIK: ";
for (int i = 0; i < counter; i++)
{
cout << S[i];
}
cout << '\n';
}
else
{
for (int i = left; i <= right; i++)
{
debug << S[left] << " : " << S[i] << '\n';
S = zamien(left, i,S);
counter++;
permutuj(left + 1, right, S,k,counter);
counter--;
debug << "COUNTER: " << counter << '\n';
S = zamien(left, i,S);
}
}
}
char* zamien(int left, int i,char*S)
{
char help = S[left];
S[left] = S[i];
S[i] = help;
return S;
}