fork download
  1. #include <iostream>
  2. using namespace std;
  3. int N, a[15];
  4. bool used[15]={};
  5.  
  6. void solve(int d) {
  7. if (d > N) {
  8. for (int i = 1; i <= N; i++) {
  9. cout << a[i] << " ";
  10. }
  11. cout << "\n";
  12. return;
  13. }
  14. for (int i = 1; i <= N; i++) {
  15. if (used[i]==1) continue;
  16. used[i] = true;
  17. a[d] = i;
  18. solve(d + 1);
  19. used[i] = false;
  20. }
  21. }
  22.  
  23. int main() {
  24. cin >> N;
  25. solve(1);
  26. return 0;
  27. }
Success #stdin #stdout 0.01s 5324KB
stdin
3
stdout
1 2 3 
1 3 2 
2 1 3 
2 3 1 
3 1 2 
3 2 1