>>586
#include <algorithm>
#include <iostream>

int factorial(int x){return x == 1 ? 1 : x * factorial(x - 1);}
int main()
{
  int a[4] = {1, 2, 3, 4},
      count = 0,
      num_a = sizeof(a) / sizeof(a[0]),
      end = factorial(num_a);
  while(count++ < end)
  {
    for(int iter = 0; iter < num_a; ++iter)
      std::cout << a[iter];
    std::cout << std::endl;
    std::next_permutation(a, a + num_a);
  }
  return 0;
}