Advertisement
mhdew

BS_01

Feb 17th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. // freopen("in.txt", "r", stdin);
  8. // freopen("Out.txt", "w", stdout);
  9.  
  10. int a[10000];
  11. int n;
  12. scanf("%d", &n);
  13. for(int i=0;i<n;i++)
  14. scanf("%d", &a[i]);
  15.  
  16. // cout<<"The number, you want to find out: ";
  17. int key;
  18. scanf("%d", &key);
  19.  
  20. int l=0;
  21. int h=n-1;
  22. int mid;
  23. int temp=0;
  24. int idx;
  25.  
  26. while(l<=h){
  27. mid=(l+h)/2;
  28. if(a[mid]==key){
  29. idx=mid;
  30. h=mid-1;
  31. temp=1;
  32. }
  33. else if(a[mid]<key){
  34. l=mid+1;
  35. }
  36. else{
  37. h=mid-1;
  38. }
  39. }
  40. printf("index=%d\n", idx);
  41. if(temp!=1)
  42. printf("Paini :/\n");
  43.  
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement