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 <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

int main(){
	int n;
	int d[120];
	while(cin>>n,n){
		memset(d, 0, sizeof(d));
		rep(i, n){
			cin>>d[i];
		}
		int x;
		cin>>x;
		int left,right,cnt;
		left =0;
		right =n-1;
		cnt = 0;
		while(right-left>=0){
			int pos =(right+left)/2;
			//cout <<"pos = "<<pos<<endl;
			if(d[pos]<x){
				cnt++;
				left =pos+1;
			}else if(d[pos]>x){
				cnt++;
				right =pos-1;
			}else{
				cnt++;
				break;
			}
		}
		cout <<cnt<<endl;
	}
	return 0;
}

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

int main() {
    while (true) {
        int n;
        cin >> n;
        if (n == 0) {
            break;
        }
        int* array = new int [n];
        for (int i = 0; i < n; i++) {
            cin >> array[i];
        }
        int key;
        cin >> key;
        int count = 0, min = 0, max = n - 1, value = -1;
        while (min <= max) {
            value = array[(max + min) / 2];
            count++;
            if (key < value) {
                max = (max + min) / 2 - 1;
            } else if (key > value) {
                min = (max + min) / 2 + 1;
            } else {
                break;
            }
        }
        cout << count << endl;
        delete[] array;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;

int main() {
	int n;
	int t[110];

	while (cin >> n, n) {
		for (int i = 0; i < n; i++) {
			cin >> t[i];
		}

		int x;
		cin >> x;

		int l = 0;
		int r = n - 1;
		int cnt = 0;

		while (l <= r) {
			cnt++;
			int m = (l + r) / 2;

			if (t[m] == x)
				break;
			if (t[m] > x) {
				r = m - 1;
			} else {
				l = m + 1;
			}
		}

		cout << cnt << endl;
	}
}

C++: #include <stdio.h>
int a, b, c;
int main() {
	while(true) {
		scanf("%d", &a);
		if(a == 0) break;
		for(int i = 0; i < a; i++) scanf("%d", &b);
		c = 0;
		while (a != 0) {
			a /= 2; c++;
		}
		printf("%d\n", c);
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int a[200];
int n;

int search(int i){
  int count = 0;
  int lix = 0,rix = n - 1;
  while(lix <= rix){
    cout << lix << rix << endl;
    int med = (lix + rix)/2;

    count++;
    if(a[med] == i)return count;

    if(a[med] < i){
      lix = med + 1;
      continue;
    }

    if(a[med] > i){
      rix = med - 1;
      continue;
    }
  }
  return count;
}

int main(){

  while(true){
    cin >> n;
    if(n == 0)break;
    for(int i = 0;i < n;i++){
      cin >> a[i];
    }
      
    int i;
    cin >> i;
    cout << search(i) << endl; 
  }
}

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

int search(int a[], int n, int k) {
    int count = 0;
    int l = 0, r = n - 1;
    while (l <= r) {
        count++;
        int m = (l + r) / 2;
        if (a[m] < k) l = m + 1;
        else if (a[m] > k) r = m - 1;
        else return count;
    }
    return count;
}

int main() {
    while (true) {
        int n;
        cin >> n;
        if (!n) return 0;
        int a[n];
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        int k;
        cin >> k;
        cout << search(a, n, k) << endl;
    }
}

C++: #include <iostream>
using namespace std;
#define rep2(x,from,to) for(int x=(from);x<(to);++(x))
#define rep(x,to) rep2(x,0,to)
int main(){
	int n, a[110], t;
	while(cin >> n, n) {
		rep(i,n) {
			cin >> a[i];
		}
		cin >> t;
		int lo = 0, hi = n-1, cnt = 0;
		while(1) {
			int mid = (hi+lo) / 2;
			cnt++;
			if(hi-lo < 1 || a[mid]==t) break;
			else if(a[mid]<t) lo = mid+1;
			else hi = mid;
		}
		cout << cnt << endl;
	}
	return 0;
}

C++: #include <stdio.h>

int main(void)
{
	int n;
	int x[100];
	int i;
	int max, min;
	int obj;
	int ans;
	
	while (1){
		scanf("%d", &n);
		
		if (n == 0)break;
		
		for (i = 0; i < n; i++){
			scanf("%d", &x[i]);
		}
		
		scanf("%d", &obj);
		
		max = n - 1;
		min = 0;
		ans = 1;
		while (1){
			
			i = (max + min) / 2;
			if (x[i] < obj){
				min = i + 1;
			}else if (x[i] > obj){
				max = i - 1;
			}else{
				break;
			}
			
			if (max == min){
				ans++;
				break;
			}
			ans++;
		}
		
		printf("%d\n", ans);
	}
	
	return (0);
}

C++: #include <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;

const int mod = 1000000007;
constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n,s;
  while(cin >> n&&n){
    ll tar;
    vary(int,v,n,0);
    rep(i,n){
      cin >> v[i];
    }
    cin >> tar;
    int lb = 0,ub = n-1;
    int ans = 0;
    while(1){
      int mid = (lb+ub)/2;
      if(lb > ub){
        break;
      }
      ++ans;
      if( tar == v[mid]) break;
      if(v[mid] < tar){
        lb = mid+1;
      }
      else{
        ub = mid-1;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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;
  while (ub != lb) {
    int mid = (ub+lb)/2;
    printf("lb=%d ub=%d mid=%d\n", lb, ub, mid);
    if (l[mid] < t) lb = mid + 1;
    else {
      if (mid != 0) ub = mid - 1;
      else ub = mid;
    }
    res++;
  }

  cout<<res<<endl;
}

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