It is limited, at the problem is not the solution, a lot of understanding
Thank you for watching this konjac
1083: binary search of an array
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1646 Solved: 641
[Submit][Status][Web Board]
Description
Check the position of the integer x integers in 1500, these numbers have been sorted from small to large. If there is output position, if there is output -1.
Input
The first row, a rear integer x 1500 lines, each line an integer
Output
An integer (for the position x, if there is output -1)
Sample Input
5
1
2
5
7
.....
Sample Output
3
HINT
Find the binary template
code:
#include<bits/stdc++.h> using namespace std; int x; int a[1501]; int main() { int left=1,right=1500,mid,find=-1; cin>>x; for(int i=1;i<=1500;i++) cin>>a[i]; while(left<=right) { mid=(left+right)/2; if(a[mid]==x){ find=mid; break; } if(a[mid]<x) { left=mid+1; } if(a[mid]>x) { right=mid-1; } } cout<<find; //freopen("yzlak.in","r",stdin); //freopen("yzlak.out","w",stdout); }