fork(5) download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4.  
  5. typedef std::vector<int> VecType;
  6. typedef std::vector<std::vector<int>> ResType;
  7.  
  8. /////////////////////////////////////////////////////////////////////////////
  9.  
  10. void Build(const size_t P, VecType T, const size_t N, const VecType& D, ResType &R) {
  11. T.push_back(D[P]);
  12. if (T.size() == N) R.push_back(T); else
  13. for(size_t i=P+1; i<D.size(); i++) Build(i,T,N,D,R);
  14. }
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17.  
  18. int main() {
  19. try {
  20. // инициализация
  21. const size_t N = 3;
  22. VecType D = {2,1,3,5,4};
  23. if (N > D.size()) throw std::logic_error(":(");
  24. ResType R = {};
  25. VecType T = {};
  26. // генерация
  27. for(size_t i=0; i<D.size()-N+1; i++) Build(i,T,N,D,R);
  28. // сортировка
  29. for(auto &i:R) std::sort(i.begin(),i.end(),std::less<int>());
  30. sort(R.begin(),R.end(),[&](auto &i,auto &j) {
  31. return std::lexicographical_compare(i.begin(),i.end(),j.begin(),j.end());
  32. });
  33. // печать
  34. for(const auto &i:R) {
  35. for(const auto &j:i) std::cout << j << " ";
  36. std::cout << std::endl;
  37. }
  38. } catch(std::logic_error &E) {
  39. std::cout << "Ошибка: " << E.what() << std::endl;
  40. } catch(...) {
  41. std::cout << "Ошибка: что-то пошло не так!" << std::endl;
  42. }
  43. return 0;
  44. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
1 2 3 
1 2 4 
1 2 5 
1 3 4 
1 3 5 
1 4 5 
2 3 4 
2 3 5 
2 4 5 
3 4 5