fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. void generate( std::vector<int>& seq, std::size_t pos = 0 )
  5. {
  6. if( pos == seq.size() ) // pos is beyond the last element; print the seq
  7. {
  8. for( int v : seq ) std::cout << v << ' ' ;
  9. std::cout << '\n' ;
  10. }
  11. else // generate sequence starting at pos
  12. {
  13. for( std::size_t i = 0 ; i <= pos ; ++i )
  14. {
  15. seq[pos] = i+1 ; // place 1,2,3 ... pos+1 at position pos
  16. generate( seq, pos+1 ) ; // and generate the rest of sequence
  17. }
  18. }
  19. }
  20.  
  21. int main()
  22. {
  23. const std::size_t N = 4 ;
  24. std::vector<int> seq(N) ;
  25. generate(seq) ;
  26. }
  27.  
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
1 1 1 1 
1 1 1 2 
1 1 1 3 
1 1 1 4 
1 1 2 1 
1 1 2 2 
1 1 2 3 
1 1 2 4 
1 1 3 1 
1 1 3 2 
1 1 3 3 
1 1 3 4 
1 2 1 1 
1 2 1 2 
1 2 1 3 
1 2 1 4 
1 2 2 1 
1 2 2 2 
1 2 2 3 
1 2 2 4 
1 2 3 1 
1 2 3 2 
1 2 3 3 
1 2 3 4