Advertisement
a53

PermPrimPF

a53
Nov 15th, 2021
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int n , x[20], A[20] , uz[20], pp[20];
  6.  
  7. bool Prim(int n)
  8. {
  9. if(n < 2)
  10. return false;
  11. if(n == 2)
  12. return true;
  13. if(n % 2 == 0)
  14. return false;
  15. for(int d = 3 ; d * d <= n ; d ++)
  16. if(n % d == 0)
  17. return false;
  18. return true;
  19. }
  20.  
  21. void Afisare(int L)
  22. {
  23. for(int i = 1 ; i <= L ; i ++)
  24. cout << A[x[i]] << " ";
  25. cout << '\n';
  26. }
  27.  
  28. bool ok(int k)
  29. {
  30. if(uz[ x[k] ] == 1)
  31. return false;
  32. if(pp[k])
  33. if(x[k] != k)
  34. return false;
  35. return true;
  36. }
  37.  
  38. void Back(int k)
  39. {
  40. for(int i = 1 ; i <= n ; i ++)
  41. {
  42. x[k] = i;
  43. if(ok(k))
  44. {
  45. uz[i] = 1;
  46. if(k == n)
  47. Afisare(n);
  48. else
  49. Back(k+1);
  50. uz[i] = 0;
  51. }
  52. }
  53. }
  54.  
  55. int main()
  56. {
  57. cin >> n;
  58. for(int i = 1 ; i <= n ; i ++)
  59. cin >> A[i], pp[i] = Prim(A[i]);
  60. for(int i = 1 ; i < n ; i ++)
  61. if(pp[i] == 0)
  62. for(int j = i + 1 ; j <= n ; j ++)
  63. if(pp[j] == 0)
  64. if(A[i] > A[j])
  65. swap(A[i] , A[j]);
  66. Back(1);
  67. return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement