aboutsummaryrefslogtreecommitdiff
path: root/challenge-054/ulrich-rieke/cpp/ch-1.cpp
blob: 9a0b50410e0ad93aabc9033c2e665ec385875128 (plain)
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
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <numeric>

int main( int argc, char * argv[] ) {
  if ( argc < 3 ) {
      std::cout << "Error! 2 integers expected!\n" ;
      return 1 ;
  }
  int limit = std::atoi( argv[1] ) ;
  int permu = std::atoi( argv[ 2 ] ) ;
  std::vector<int> numbers( limit ) ;
  std::iota( numbers.begin( ) , numbers.end( ) , 1 ) ;
  std::vector<std::vector<int> > permutations ;
  do {
      permutations.push_back( numbers ) ;
  } while ( std::next_permutation( numbers.begin( ) , numbers.end( )) ) ;
  if ( permutations.size( ) < permu ) {
      std::cout << "[]" << std::endl ;
  }
  else {
      std::cout << "[ " ;
      for ( int i : permutations[ permu - 1 ] ) {
    std::cout << i << " " ;
      }
      std::cout << "]\n" ;
  }
  return 0 ;
}