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>
#include<algorithm>

using namespace std;

const int MAX = 101;
int data[MAX];
int n;
int ans;
int wanted;

void solve(){
  int start = 0;
  int end = n-1;
  //sort(data,data+n);
  while(1){
    int now = (start+end)/2;
    if(data[now] == wanted || start == end) break;
    if(data[now] > wanted) end = now-1;
    else start=now+1;
    ans++;
  }

}

int main(){
  while(cin >> n && n){
    ans = 1;
    for(int i = 0; i < n; i++) cin >> data[i];
    cin >> wanted;
    solve();
    cout << ans << endl;
  }
  return 0;
}

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

using namespace std;

int s[100010],t[50010];
int n, q, ans = 0;

bool binary_search(int x) {
	int l = 0, r = n-1;
	ans = 0;
	while (r - l >= 1) {
		int m = (l + r) / 2;
		ans++;
		if (s[m] == x)return true;
		else if (s[m] > x) {
			r = m-1;
		}
		else if (s[m] < x) {
			l = m+1;
		}
	}
	ans++;
	return false;
}
int main() {
	while (cin >> n, n) {
		for (int i = 0; i < n; i++) {
			cin >> s[i];
		}
		cin >> t[0];
		binary_search(t[0]);
		for (int i = 0; i < n; i++) {
			s[i] = 0;
		}
			cout << ans << endl;
	}
	return 0;
}