Advertisement
sve_vash

Untitled

Jul 12th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. void perm(vector<int> num, int n, int pos, vector<vector<int>> &perms) {
  9. if (pos >= n) {
  10. perms.push_back(num);
  11. return;
  12. }
  13. else {
  14. for (int i = pos; i < n; i++) {
  15. swap(num[i], num[pos]);
  16. perm(num, n, pos + 1, perms);
  17. swap(num[i], num[pos]);
  18. }
  19. }
  20. }
  21.  
  22. int main() {
  23. unsigned int n;
  24. ifstream inp("input.txt");
  25. inp >> n;
  26. inp.close();
  27.  
  28. vector<int> p(n);
  29. vector<vector<int>> perms;
  30.  
  31. ofstream out("output.txt");
  32. for (int i = 0; i < n; ++i)
  33. p[i] = i + 1;
  34.  
  35. perm(p, n, 0, perms);
  36.  
  37. sort(perms.begin(), perms.end());
  38.  
  39. for (int i = 0; i < perms.size(); ++i) {
  40. for (int j = 0; j < n; ++j) {
  41. out << perms[i][j] << ' ';
  42. }
  43. out << "\n";
  44. }
  45.  
  46.  
  47. out.close();
  48. return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement