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 <stdio.h>
#include <cctype>
#include <limits.h>
#include <math.h>
#include <complex>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16> >(x);
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)
#define if_range(x, y, w, h) if (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))

const int INF = 1000000000;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4]={-1, 0, 1, 0}, dy[4]={0, -1, 0, 1};
using namespace std;
typedef pair<int, int> P;

/** Problem0188 : Search **/
int main()
{
	int N;
	while (cin>>N, N) {
		vector<int> a(N);
		rep(i, N) cin>>a[i];
		int t; cin>>t;
		int ans=0;
		
		int l=0, r=N-1;
		while (l<=r) {
			ans++;
			int m=(l+r)/2;
			if (a[m] == t) {
				break;
			} else if (a[m]> t) {
				r = m-1;
			} else {
				l = m+1;
			}
		}
		cout << ans << endl;
	}
}

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

int main() {
	int n;
	while(cin >> n, n) {
		vector<int> v;
		int f;
		for(int i = 0; i < n; i++) {
			int tmp;
			cin >> tmp; v.push_back(tmp);
		}
		cin >> f;

		int l, r;
		l = 0; r = v.size() - 1;
		int ans = 0;
		while(l <= r) {
			int m;
			m = (l + r) / 2;
			ans++;
			if(v[m] == f)
				break;
			else if(v[m] < f)
				l = m + 1;
			else
				r = m - 1;
		}

		cout << ans << endl;
	}
}

C++: // 2011/07/07 Tazoe

#include <iostream>
using namespace std;

int main()
{
	while(true){
		int n;
		cin >> n;
		if(n==0)
			break;

		int data[100];
		for(int i=0; i<n; i++)
			cin >> data[i];

		int key;
		cin >> key;

		int cnt = 0;
		for(int low=0, high=n-1; low<=high;){
			int middle = (low+high)/2;
			cnt++;
			if(key==data[middle])
				break;
			else if(key<data[middle])
				high = middle-1;
			else
				low  = middle+1;
		}

		cout << cnt << endl;
	}

	return 0;
}

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

int main()
{
  int n;
  for(;cin>>n,n;)
    {
      int data[n];
      for(int i=0;i<n;i++)
	{
	  cin>>data[i];
	}
      int s;
      cin>>s;
      int hi=0,lo=n-1;
      int cnt=0;
      while(true)
	{
	  int p = (hi+lo)/2;
	  cnt++;
	  //  cout<<hi<<" "<<lo<<" "<<cnt<<endl;
	  if(data[p]==s)
	    break;
	  if(hi==lo)
	    break;	 
	  if(data[p]<s)
	    hi=p+1;
	  else
	    lo=p-1;
	}
      cout<<cnt<<endl;
    }
}

C++: #include <stdio.h>
main(){
  while(1){
  int a,n[100],i,key,l=0,r,mid,c=0;
    scanf("%d",&a);
    if(a==0) break;
    for(i=0;i<=a;i++){
      scanf("%d",&n[i]);
    }
    key=n[a];
    r=a-1; 
    while(l<=r){
      mid=(l+r)/2;
      c++;
      if(n[mid]==key) break;
      else if(n[mid]<key) l=mid+1;
      else r=mid-1;
    }
    printf("%d\n",c);
  } 
  return 0; 
}

C++: #include <cstdio>
#include <cmath>
int n, x;
int a[100];
int main() {
    while (scanf("%d",&n)) {
        if (!n) break;
        for (int i=0; i<n; i++) scanf("%d",&a[i]);
        scanf("%d", &x);
        int lb=0, ub=n-1, res=0;
        while (ub-lb>=1) {
            res++;
            int mid=floor((lb+ub)/2);
            if (a[mid]<x) {
                lb=mid+1;
            } else if (a[mid]>x) {
                ub=mid-1;
            } else {
                break;
            }
        }
        printf("%d\n",res+1);
    }
}

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

const int MAX_N = 100;
int values[MAX_N];

int main(){
	int n;
	while(scanf("%d", &n), n != 0){
		// target: 探す値, ans: 探索が終わるまでの比較回数
		int target, ans = 0;
		// 二分探索の範囲
		int l = 0, r = n - 1;
		
		memset(values, 0, sizeof(values));
		for (int i = 0; i < n; i++) {
			scanf("%d", &values[i]);
		}
		scanf("%d", &target);
		
		while(l <= r) {
			ans++;
			int m = (l + r) / 2;
			if (values[m] == target) break;
			else if (values[m] < target) l = m + 1;
			else r = m - 1;
		}
		printf("%d\n", ans);
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
int n;
while(cin>>n,n){
  int a[n];
  for (int i=0;i<n;++i){
    cin>>a[i];
  }
  
  int goal;
  cin>>goal;
  int left,right;
  
  left=0;
  right=n;
  if(a[n/2]==goal){
    cout<<1<<endl;
    continue;
  }
  int sum=1;
  while(left<right){
    if( a[(left+right)/2]==goal )break;
    if(a[(left+right)/2]>goal){right=(left+right)/2-1;}
    if(a[(left+right)/2]<goal){left=(left+right)/2+1;}
    sum++;
  }
  //if(left==right){
    cout<<sum<<endl;
    //}
  
  
 }
 return 0;
}

C++: #define _USE_MATH_DEFINES
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<climits>
using namespace std;

int main(){
	int N;
	while(cin >> N, N){
		int A[100], Target;
		for(int i = 0; i < N; i++) cin >> A[i];
		cin >> Target;

		int l = 0, r = N - 1, piv;
		int cnt = 0;
		while(r - l >= 0){
			cnt++;
			int mid = (l + r)/2;
			if(A[mid] > Target) r = mid - 1;
			else if(A[mid] < Target) l = mid + 1;
			else break;
		}
		printf("\t%d\n", cnt);
	}
	return 0;
}

C++: #include <iostream>

using namespace std;

#define MAX_N 101

int a[MAX_N];

int main() {
    int n, f;
    while (cin >> n) {
        if (n == 0) break;
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        cin >> f;
        int count = 0;
        int r = 0, l = n-1;
        int mid;
        while (true) {
            mid = (r+l) / 2;
            count++;
            if (a[mid] == f) {
                 break;
            } else {
                if (mid == r) {
                    count++;
                    break;
                }
            }
            if (a[mid] < f) {
                r = mid+1;
            }
            if (a[mid] > f) {
                l = mid-1;
            } 
        }
        cout << count << endl;
    }
    return 0;
}