#include <iostream>
#include <vector>
void generate( std::vector<int>& seq, std::size_t pos = 0 )
{
if( pos == seq.size() ) // pos is beyond the last element; print the seq
{
for( int v : seq ) std::cout << v << ' ' ;
std::cout << '\n' ;
}
else // generate sequence starting at pos
{
for( std::size_t i = 0 ; i <= pos ; ++i )
{
seq[pos] = i+1 ; // place 1,2,3 ... pos+1 at position pos
generate( seq, pos+1 ) ; // and generate the rest of sequence
}
}
}
int main()
{
const std::size_t N = 4 ;
std::vector<int> seq(N) ;
generate(seq) ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKdm9pZCBnZW5lcmF0ZSggc3RkOjp2ZWN0b3I8aW50PiYgc2VxLCBzdGQ6OnNpemVfdCBwb3MgPSAwICkKewogICBpZiggcG9zID09IHNlcS5zaXplKCkgKSAvLyBwb3MgaXMgYmV5b25kIHRoZSBsYXN0IGVsZW1lbnQ7IHByaW50IHRoZSBzZXEKICAgewogICAgICAgZm9yKCBpbnQgdiA6IHNlcSApIHN0ZDo6Y291dCA8PCB2IDw8ICcgJyA7CiAgICAgICBzdGQ6OmNvdXQgPDwgJ1xuJyA7CiAgIH0KICAgZWxzZSAvLyBnZW5lcmF0ZSBzZXF1ZW5jZSBzdGFydGluZyBhdCBwb3MKICAgewogICAgICAgZm9yKCBzdGQ6OnNpemVfdCBpID0gMCA7IGkgPD0gcG9zIDsgKytpICkKICAgICAgIHsKICAgICAgICAgICBzZXFbcG9zXSA9IGkrMSA7IC8vIHBsYWNlIDEsMiwzIC4uLiBwb3MrMSBhdCBwb3NpdGlvbiBwb3MKICAgICAgICAgICBnZW5lcmF0ZSggc2VxLCBwb3MrMSApIDsgLy8gYW5kIGdlbmVyYXRlIHRoZSByZXN0IG9mIHNlcXVlbmNlCiAgICAgICB9CiAgIH0KfQoKaW50IG1haW4oKQp7CiAgICBjb25zdCBzdGQ6OnNpemVfdCBOID0gNCA7CiAgICBzdGQ6OnZlY3RvcjxpbnQ+IHNlcShOKSA7CiAgICBnZW5lcmF0ZShzZXEpIDsKfQo=