Advertisement
ilnazEPTA

Untitled

Dec 22nd, 2019
89
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. using namespace std;
  3. void quick_sort(int* a, int l, int r)
  4. {
  5. int pe;
  6. int l_hold = l;
  7. int r_hold = r;
  8. pe = a[l];
  9. while (l < r)
  10. {
  11. while ((a[r] >= pe) && (l < r))
  12. r--;
  13. if (l != r)
  14. {
  15. a[l] = a[r];
  16. l++;
  17. }
  18. while ((a[l] <= pe) && (l < r))
  19. l++;
  20. if (l != r)
  21. {
  22. a[r] = a[l];
  23. r--;
  24. }
  25. }
  26. a[l] = pe;
  27. pe = l;
  28. l = l_hold;
  29. r = r_hold;
  30. if (l < pe)
  31. quick_sort(a, l, pe - 1);
  32. if (r > pe)
  33. quick_sort(a, pe + 1, r);
  34. }
  35. int main()
  36. {
  37. int n;
  38. cout << "Enter n:";
  39. cin >> n;
  40. int t;
  41. cout << "Enter t:";
  42. cin >> t;
  43.  
  44. int* a = new int[n];
  45. for (int i = 0; i < n; i++)
  46. cin >> a[i];
  47.  
  48. quick_sort(a, 0, n - 1 );
  49.  
  50.  
  51. int i = 0;
  52. int j = n - 1;
  53. int res = 0;
  54.  
  55. while (i < j)
  56. {
  57. if (a[i] + a[j] > t)
  58. j--;
  59. if (a[i] + a[j] < t)
  60. i++;
  61. else
  62. {
  63. res++;
  64. i++;
  65. j--;
  66. }
  67.  
  68. }
  69. cout << res;
  70.  
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement