Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int a[100], x;
- int bin_search( int L, int R ){
- int k = (L+R)/2;
- // if(( x > a[k] && x > a[R] ) || ( x < a[k] && x < a[L])) return -1; //stupid
- if( L > R ) return -1;
- if( x == a[k] ) return k;
- x > a[k] ? bin_search( k+1, R ) : bin_search( L, k-1 );
- }
- int main(){
- for( int i=0; i<10; i++ ) a[i] = 2*i;
- for( int i=0; i<10; i++ ) cout <<i << ": "<< a[i] << endl;
- while( cin >> x ){
- cout << bin_search( 0, 9 ) << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment