irapilguy

Untitled

Nov 9th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. using namespace std;
  4. #define N 10000
  5. int mas[N];
  6. int BS(int l, int r, int key) {
  7. int mid = 0;
  8. if (l >= r) return -1;
  9. mid = (l + r) / 2;
  10. if (mas[mid] == mas[key]) return mid;
  11. if (mas[l] == mas[key]) return l;
  12. if (mas[r] == mas[key]) return r;
  13. if (mas[mid] < mas[key]) return BS(mid + 1, r, key);
  14. if (mas[mid] > mas[key]) return BS(l, mid - 1, key);
  15. return -1;
  16. }
  17. int main() {
  18. int n;
  19. cin >> n;
  20. for (int i = 0; i < n; i++) {
  21. cin >> mas[i];
  22. }
  23. cout << BS(0, n - 1, 3);
  24. return 0;
  25. }
Add Comment
Please, Sign In to add comment