Go: // binary search

package main

import (
	"bufio"
	"os"
	"strconv"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		a := make([]int, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			a[i], _ = strconv.Atoi(scanner.Text())
		}
		scanner.Scan()
		k, _ := strconv.Atoi(scanner.Text())
		cnt := 0
		low, high := 0, n-1
		for low <= high {
			cnt++
			mid := (low+high)>>1
			if k == a[mid] {
				break
			}
			if k < a[mid] {
				high = mid-1
			} else {
				low = mid+1
			}
		}
		fmt.Println(cnt)
	}
}

C++: #include <iostream>
using namespace std;

int main(){
    int n;
    while(cin >> n, n != 0){
        int data[n];
        int value;
        for(int i=0; i<n; i++){
            cin >> data[i];
        }
        cin >> value;
        int left = 0;
        int right = n-1;
        int mid = 0;
        int cnt = 0;
        while(left <= right){
            mid = (left+right) / 2;
            if(value > data[mid]){
                cnt++;
                left = mid+1;
            }else if(value < data[mid]){
                cnt++;
                right = mid-1;
            }else{
                cnt++;
                break;
            }
        }
        cout << cnt << endl;
    }
    return 0;
}

C++: #include<iostream>
using namespace std;
int a[100];
int main(){
	int n,key;
	while(true){
		cin>>n;
		if(n==0)break;
		for(int i=0;i<n;i++)cin>>a[i];
		cin>>key;
		int ans=1;
		int left=0,right=n,mid;
		while(true){
			if(left>right)break;
			mid=(left+right)/2;
			if(a[mid]==key)break;
			if(a[mid]<key)left=mid+1;
			if(a[mid]>key)right=mid-1;
			ans++;
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>

using namespace std;

void solve()
{
	int n;
	while(cin >> n, n)
	{
		vector<int> Vec(n);
		for(int i = 0; i < n; ++i)
		{
			cin >> Vec[i];
		}
		int target;
		cin >> target;
		int left = 0;
		int right = n - 1;
		int count = 0;
		while(right >= left)
		{
			count++;
			int m = (left + right) / 2; 
			if(target == Vec[m])
			{
				break;
			}
			target < Vec[m] ? right = m - 1 : left = m + 1;
		}
		cout << count << endl;
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include <cstdio>
#define buf 1000001
using namespace std;
int cnt,b[buf];
void binary_search(int d,int a);

int main(void){
  int i,j,a,n;
  char str[200];

  for( ; ; ){
    fgets(str,sizeof(str),stdin);
    sscanf(str,"%d",&a);
    if(a==0) break;
    for(i=0;i<a;i++){
      fgets(str,sizeof(str),stdin);
      sscanf(str,"%d",&n);
      b[i]=n;
    }
    for(i=0;i<a;i++)
      printf("%d ",b[i]);
    printf("\n");
    cnt=1;
    fgets(str,sizeof(str),stdin);
    sscanf(str,"%d",&n);
    binary_search(n,a);
    printf("%d\n",cnt);
  }
  return 0;
}

void binary_search(int n,int a){
  int left,right,mid;
  left = 0; right = a-1;
  while(left < right){
    mid = (left + right)/2;
    if(b[mid] == n)
      break;
    else if(n < b[mid])
      right = mid;
    else if(n > b[mid])
      left = mid+1;
    cnt++;
  }
}

C++: #include <iostream>
#include <cstdio>
using namespace std;

#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n) REP(i,0,n)

int n, l[100], t;

void solve() {
  rep(i,n) cin>>l[i];
  cin>>t;
  
  int lb = 0, ub = n-1, res = 1, md = (lb+ub)/2;
  while (ub != lb && l[md] != t) {
    res++;
    
    md = (lb+ub)/2;
    //printf("lb=%d ub=%d md=%d\n", lb, ub, md);
    if (l[md] < t) {
      lb = md + 1;
    } else if (l[md] > t) {
      ub = md - 1;
    }
  }
  cout<<res<<endl;
}

int main (int argc, char const* argv[]) {
  while (cin>>n, n) {
    solve();
  }
  return 0;
}

C++: #include<iostream>
#include<vector>

std::vector<int> v;

int main()
{
	int n;
	while( std::cin >> n, n )
	{
		v.clear();

		for( int i = 0; i != n; ++i )
		{
			int a;
			std::cin >> a;
			v.push_back( a );
		}

		int b;
		std::cin >> b;

		int ans = 0;
		int l = 0, r = v.size() - 1;
		while( l <= r )
		{
			int mid = ( l + r ) / 2;

			++ans;

			if( r <= l )
				break;

			if( v[mid] < b )
				l = mid + 1;
			else if( v[mid] > b )
				r = mid - 1;
		}

		std::cout << ans << std::endl;
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;

int main(){
    int n;
    while(cin >> n, n){
        vector<int> v(n);
        for(int i=0; i < n; i++) cin >> v[i];
        int k; cin >> k;
        int lb = -1, ub = n, count = 0;
        while (ub - lb > 1) {
            int mid = (lb + ub) / 2;
            if (v[mid] >= k) {
                ub = mid;
            } else {
                lb = mid;
            }
            count++;
        }
        cout << count << endl;
    }
}

C++: #include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second

template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pll> pip;
const ll INF = 1ll<<60;
const ll MOD = 1000000007;
const double EPS  = 1e-9;

int n;
int a[112345];

int main() {
	while (cin >> n, n) {
		REP(i, n) scanf("%d", a + i);
		
		int k; cin >> k;
		
		int l = 0, r = n - 1;
		int c = 0;
		while (r - l >= 0) {
			int m = (l + r) / 2;
			c++;
			
			if (a[m] == k) break;
			if (a[m] < k) l = m + 1;
			else r = m - 1;
		}
		
		cout << c << endl;
	}
	return 0;
}

C++: #include <iostream>
using namespace std;

int main(void){
	int i,n,x[104],search;
	int l,r,c;
	int count;

	while(cin>>n && n){
		for(i=0;i<n;i++) cin>>x[i];
		cin>>search;

		for(count=l=0,r=n-1,c=(l+r)/2;l<=r;c=(l+r)/2,count++){
			if(search < x[c]) r = c - 1;
			else if(search > x[c]) l = c + 1;
			else{count++; break;}
		}
		cout<<count<<endl;
	}

	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS

#include<fstream>
#include<iostream>
#include<string>
#include<iomanip>
#include<list>
#include<math.h>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<utility>

using namespace std;

int main(){
	int n = 0;
	int num[100];
	while (cin >> n && n != 0) {
		int now = n >> 1;
		int n_cnt = n;
		int sea;
		int cnt = 0;
		for (int i = 0; i < n; i++) {
			cin >> num[i];
		}
		cin >> sea;
		while (n_cnt > 0) {
			cnt++;
			n_cnt = n_cnt >> 1;
			if (num[now] == sea) {
				break;
			}
			else if (num[now] > sea) {
				now -= (n_cnt - (n_cnt >> 1));
			}
			else {
				now += (n_cnt - (n_cnt >> 1));
			}
		}
		cout << cnt << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while(cin>>n,n){
        int num[n],ans;
        for(int i=0;i<n;i++)cin>>num[i];
        cin>>ans;
        int serch=n/2,kawa[2]={0,n};
        for(int i=1;;i++){
            if(num[serch]==ans||i==n-1){
                cout<<i<<endl;
                break;
            }
            else if(num[serch]>ans){
                    kawa[1]=serch;
                    serch=(serch+kawa[0])/2;
            }
            else {
                    kawa[0]=serch;
                    serch=(serch+kawa[1])/2;
            }
        }
    }
}

C++: #include<stdio.h>
#include<string.h>
#include<math.h>

int main(){
	int tan[1100],i,j,n,t,c;
	
	while(scanf("%d",&n)){
		if(n==0)break;
		
		for(i=0;i<n;i++){
			scanf("%d",&tan[i]);
		}
		scanf("%d",&t);
		
		i=0;
		j=n-1;
		c=0;
		while(i<=j){
			++c;
			if(tan[(i+j)/2]<t){
				i=(i+j)/2+1;
			}else if(tan[(i+j)/2]==t){
				break;
			}else{
				j=(i+j)/2-1;
			}
		}
		printf("%d\n",c);
	}
	return 0;
}