C++ Library Extensions 2022.12.09
To help learn modern C++ programming
062-strategy.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2#include <tpf_ncrnpr.hpp>
3
4// #include<ios>
8
9using element_t = size_t;
10using set_t = std::deque<element_t>;
11
12void build_permutation(set_t S, set_t& R, size_t r, size_t m_th)
13{
14 if(S.empty() || r == 0) return;
15
16 auto n_1_p_r_1 = tpf::ncrnpr::npr(S.size()-1, r-1);
17
18 auto quotient = m_th / n_1_p_r_1;
19
20 R.push_back(S[quotient]);
21
22 S.erase(S.begin() + quotient);
23
24 build_permutation(S, R, r-1, m_th % n_1_p_r_1);
25}
26
27void test_permutation(size_t n, size_t r)
28{
29 set_t S(n);
30
31 std::iota(S.begin(), S.end(), 1);
32
33 stream << " The set S : "<< S << endL;
34
35 auto max_m_th = tpf::ncrnpr::npr(n, r);
36
37 for(size_t m_th = 0; m_th < max_m_th; ++m_th)
38 {
39 set_t R;
40 build_permutation(S, R, r, m_th);
41
42 stream <<" m_th -> "<< std::setw(2) << m_th <<" : " << R << endl;
43 }
44}
45
46int main()
47{
48 // we define a set S = { 1, 2, 3, 4};
49 // from this set, we choose 4 elements and enumerate all permutations
50 test_permutation(4, 4);
51
52 stream << endL;
53
54 // from a set S with 4 elements,
55 // we choose 3 elements and enumerate all permutations
56
57 test_permutation(4, 3);
58
59}
60
std::deque< element_t > set_t
Definition: 061-subsets.cpp:9
auto endL
Definition: 062-strategy.cpp:7
void build_permutation(set_t S, set_t &R, size_t r, size_t m_th)
tpf::sstream stream
Definition: 062-strategy.cpp:5
void test_permutation(size_t n, size_t r)
auto endl
Definition: 062-strategy.cpp:6
int main()
enable_if_all_in_list_t< types::type_list_t< Type1, Type2 >, integral_list_t, ReturnType > npr(Type1 nn, Type2 rr)
Definition: tpf_ncrnpr.hpp:428
constexpr auto endL
Definition: tpf_output.hpp:974
constexpr auto endl
Definition: tpf_output.hpp:973
Stream output operators << are implemented.