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 <cstdio>
#include <algorithm>
using namespace std;

int main(){
  int n,key,num[1000]={0};
  while( cin >> n,n ){
    for(int i = 0 ; i < n ; i++ ){
      cin >> num[i];
    }
    cin >> key;
    sort(num,num+n);
    int r = n-1;
    int l = 0;
    int cnt = 0;
    while(l <= r){
      int c = (r + l)/2;
      cnt++; 
      if(num[c] == key){break;}
      else if(num[c] < key){l = c+1;}
      else {r = c-1;}
    }
    cout << cnt << endl;
  }
  return 0;
}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
const double EPS = 1e-10;

signed main() {
	int n;
	while (cin >> n, n) {
		vector<int>v(n); rep(i, 0, n)cin >> v[i];
		int k; cin >> k;
		int ans(0);
		int l = 0, r = n - 1;
		while (l <= r) {
			int mid = (l + r) / 2;
			ans++;
			if (v[mid] == k)break;
			else if (v[mid] < k) l = mid + 1;
			else r = mid - 1;
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>

int search(int *n, int key, int p, int l, int count){
	int c = (p+l)/2;
	if(n[c] == key || p>=l)
		return count+1;
	else if(n[c] < key)
		return search(n, key, c+1, l, count+1);
	else
		return search(n, key, p, c-1, count+1);
}

int main(){
	int *data, n, key;
	while(true){
		std::cin >> n;
		if(n==0)
			return 0;
		data = new int[n];
		for(int i=0; i<n; i++)
			std::cin >> data[i];
		std::cin >> key;
		std::cout << search(data, key, 0, n-1, 0) << std::endl;
		delete[] data;
	}
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

int main(){
  int n;
  while(cin>>n, n){
    vector<int> v(n);
    REP(i,n) cin>>v[i];
    
    int cnt = 0;
    int low = 0, high = n;
    int s; cin>>s;
    while(true){
      int mid = (low+high)/2;
      //cout << "mid=" << mid << endl;
      cnt++;
      if(v[mid] == s) break;
      if(low >= high) break;
      if(v[mid] > s){
	high = mid-1;
      }else{
	low  = mid+1; 
      }
    }
    cout << cnt << endl;
  }
  return 0;
}

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

int T[100010];
int find(int m,int x){
	int r=0,l=m,ans=0;
	while(1){
		ans++;	
		if(r>=l)break;
		if(T[(r+l)/2]==x)return ans;
		if(T[(r+l)/2]>x)l=(r+l)/2-1;
		else if(T[(r+l)/2]<x)r=(r+l)/2+1;
	}
	return ans;
}
int main(){
	int n,k,num,ans;
	while(1){
		cin>>n;
		if(n==0)break;
		for(int i=0;i<n;i++){
			cin>>T[i];
		}
		cin>>k;
		ans=find(n,k);
		cout<<ans<<endl;
	}
	return 0;
}

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

int main(){
	int n, s, cnt, bottom, mid, top;
	vector< int > seq;
	
	while(cin >> n, n){
		seq.clear();
		seq.resize(n);
		for(int i = 0; i < n; ++i){
			cin >> seq[i];
		}
		cin >> s;
		
		cnt = 0;
		
		bottom = 0;
		top = n - 1;
		mid = (top + bottom) / 2;
		
		while(1){
			++cnt;
			
			if(top - bottom <= 0) break;
			
			if(seq[mid] == s){
				break;
			}else if(seq[mid] > s){
				top = mid - 1;
			}else if(seq[mid] < s){
				bottom = mid + 1;
			}
			
			mid = (top + bottom) / 2;
		}
		
		cout << cnt << 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 = 1;
		while(target != Vec[(left + right) / 2] && right > left)
		{
			cout << left << " " << right << endl;
			count++;
			if(target < Vec[(left + right) / 2])
			{
				right = (left + right) / 2 - 1;
			}
			else
			{
				left = (left + right) / 2 + 1;
			}
		}
		cout << count << endl;
	}
}

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

C++: #include <cstdio>
#include <utility>
#include <vector>
#include <functional>
using namespace std;

#define _gp(l) const auto gcu{getchar##l}; const auto pcu{putchar##l}
#ifdef __linux
_gp(_unlocked);
#else
_gp();
#endif
#define _DEF(r, n, ...) inline r n(__VA_ARGS__) noexcept
#define _T template <typename T>
#define _HT template <typename H,typename... T>
#define _OP(t) inline operator t()
struct _in {
#ifdef _GLIBCXX_STRING
	_OP(string){string s;for(char c;c=gcu(),c!=' '&&c!='\n';)s+=c;return s;}
#define _S
#endif
	_OP(char){char c=gcu();gcu();return c;}
	_OP(double){double d; scanf("%lf",&d); gcu();return d;}
	_T _OP(T){T n{},m{1},c;if((c=gcu())=='-')m=-1,c=gcu();do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m*n;}
} in;
#define _SCAN(...) _DEF(bool,scan,__VA_ARGS__)
#ifdef _S
_SCAN(string &o) {int c{gcu()};if(c==EOF)return false;else{ungetc(c,stdin);string t=move(in);o=t;return true;}}
#endif
_T _SCAN(T &o) {int c{gcu()};return c==EOF?false:(ungetc(c,stdin),o=in,true);}
_HT _SCAN(H &h,T&&... t){return scan(h)&&scan(t...);}
#define _OUT(...) _DEF(void,out,__VA_ARGS__)
#define _OUTL(...) _DEF(void,outl,__VA_ARGS__)
_OUT(bool b){pcu('0'+b);}
_OUT(const char *s){while(*s)pcu(*s++);}
_OUT(char c){pcu(c);}
#ifdef _S
_OUT(string &s){for(char c:s)pcu(c);}
#endif
_T _OUT(T n){static char b[20];char *p=b;T m=n<0?pcu('-'),-1:1;
	if(!n)*p++='0';else while(n)*p++=(char)(n%10*m+'0'),n/=10;while(p!=b)pcu(*--p);}
	_OUTL(){out('\n');}
#ifdef _GLIBCXX_VECTOR
	_T _OUT(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);}
#endif
	_HT _OUT(H &&h, T... t){out(h);out(t...);}
	template <typename... T> _OUTL(T... t){out(t...);outl();}
	struct range{
		int e,b=0,s=1;range(int _b,int _e,int _s):e(_e),b(_b),s(_s){} range(int _b,int _e): e(_e), b(_b){} range(int _e):e(_e){}
		struct it{int v,s; it(int _v,int _s):v(_v),s(_s){} operator int()const{return v;} operator int&(){return v;}int operator*()const{return v;}
			it& operator++(){v+=s;return *this;} }; it begin(){return {b,s};} it end(){return {e,s};}};
#define times(i,n) for(int i=n;i;i--)

using V = vector<int>;

int main() {
	for (int n; (n = in);) {
		V v(n);
		for (int &i: v)
			i = in;
		int s {in}, c {};
		function<int(int, int)> search {[&](int b, int e) {
			if (b > e)
				return c;
			int m = (b + e) / 2;
			c++;
			return v[m] == s ? c : s < v[m] ? search(b, m - 1) : search(m + 1, e);
		}};
		outl(search(0, v.size() - 1));
	}
}