Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. #define SIZE 20
  6.  
  7. int solutie[SIZE], n;
  8.  
  9. bool valid(int k)
  10. {
  11. for (int i = 1; i < k; i++)
  12. if (solutie[i] == solutie[k])
  13. return false;
  14.  
  15. if (k <= n / 2) {
  16. for (int i = 1; i < k; i++)
  17. if (solutie[i] > solutie[i + 1])
  18. return false;
  19. }
  20. else {
  21. for (int i = n / 2 + 1; i < k; i++)
  22. if (solutie[i] < solutie[i + 1])
  23. return false;
  24. }
  25. return true;
  26.  
  27. }
  28.  
  29. void afis()
  30. {
  31. for (int i = 1; i <= n; i++)
  32. cout << solutie[i] << ' ';
  33. cout << endl;
  34. }
  35.  
  36. void BKT(int k)
  37. {
  38. for (int i = 1; i <= n; i++) {
  39. solutie[k] = i;
  40. if (valid(k)) {
  41. if (k == n)
  42. afis();
  43. else BKT(k + 1);
  44. }
  45. }
  46. }
  47.  
  48.  
  49. int main()
  50. {
  51. cout << "Dati numarul de elemente : ";
  52. cin >> n;
  53.  
  54. for (int i = 0; i < n; i++) {
  55. cout << "A[" << i << "] = ";
  56. cin >> solutie[i];
  57. }
  58.  
  59. BKT(1);
  60.  
  61.  
  62.  
  63.  
  64. system("pause");
  65. return 1;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement