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++: import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		new Main().run();
	}
	void run(){
		Scanner sc = new Scanner(System.in);
		while(true){
			int b = sc.nextInt();
			if(b == 0){
				//System.out.println("break");
				break;
			}
			int[] a = new int[b];
			int left,right,searchvalue;
			for(int i=0; i < a.length; i++){
				a[i] = sc.nextInt();
			}
			searchvalue = sc.nextInt();
			left = 0; right = a.length-1;
			System.out.println(solve(left, right, searchvalue, a));
		}
	}

	int solve(int left, int right,int sv,int[] a){
		int index = (left + right) / 2;
		int cnt;
		//System.out.println("left:"+left+" right:"+right+" sv:"+sv+" a[index]:"+a[index]);
		if(a[index] == sv){
			return 1;
		}else if(left == right){
			return 1;
		}else if(a[index] < sv){
			left = index;
			return 1 + solve(++left, right, sv, a);
		}else if(a[index] > sv){
			right = index;
			return 1 + solve(left, --right, sv, a);			
		}
		return 0;
		
		/*
		if(cnt == a.length-1){
			System.out.println(cnt);
			return;
		}
		if(a[(left + right) / 2] < sv){
			System.out.println("left:"+left+" right:"+right);
			if(left <= right){
				left = (left + right) / 2 + 1;
				solve(left, right,sv, a, ++cnt);
			}else{
				System.out.println(++cnt);
			}
		}else if(a[(left + right) / 2] > sv){
			System.out.println("left:"+left+" right:"+right);
			if(left < right){
				right = (left + right) / 2 - 1;
				solve(left, right,sv, a, ++cnt);
			}else{
				System.out.println(++cnt);
			}
		}else{
			System.out.println(++cnt);
		}
		*/
	}
}

C++: //
#include <iostream>
#include <math.h>
using namespace std;




int main(){
    int a, b, times;
    int i1, tyuusinn, yousosuu, yousosuu2;
    while(1){
    cin >> a;
    if(a==0){break;}
    i1=0;
        times=0;
    int w[a];
    while(1){
        cin >> b;
        w[i1]=b;
        i1++;
        if(i1==a)break;
    }
    sort(w, w+a);
    cin >> b;
    yousosuu=a;
    
    tyuusinn=yousosuu/2;
        while(1){
    times++;
    yousosuu2=yousosuu/2;
    if(w[tyuusinn]==b){break;}
    else if(w[tyuusinn]>b){
        tyuusinn=tyuusinn-yousosuu/2+yousosuu2/2;
        yousosuu=yousosuu2;
    }
    else{
        if(yousosuu%2==0){
            tyuusinn=tyuusinn+yousosuu2/2;
            yousosuu=yousosuu2-1;
        }
        else{
            tyuusinn=tyuusinn+yousosuu2/2+1;
            yousosuu=yousosuu2;
        }
    }
            if(yousosuu==0){break;}
        }
        
    cout << times << 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("%d\n", cnt);
	}
	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(true){
			cnt++;
			if(l == r) break;
			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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> P;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
int data[101];
int bs(int head,int tail,int m, int n){
  int mid = (head+tail)/2;
  if(head>tail) return(n-1);
  if(data[mid]>m){
    return bs(head,mid-1,m,n+1);
  }else if(data[mid]<m){
    return bs(mid+1,tail,m,n+1);
  }else{
    return (n);
  }
}
int main(void){
  int n;
  while(cin>>n){
    if(n==0) break;
    int m;
    REP(i,n)cin>>data[i];
    cin>>m;
    if(n==1) cout<<1<<endl;
    else cout<<bs(0,n-1,m,1)<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char **argv)
{
	int n, *a, t, m, c, p, q;
	
	while(1){
		cin >> n;
		if(n == 0){
			break;
		}
		a = (int *)calloc(n, sizeof(int));
		
		for(int i = 0; i < n; i++){
			cin >> a[i];
		}
		cin >> m;
		p = c = 0;
		q = n-1;
		t = (n-1+p) / 2;
		while(t != p){
			if(a[t] < m){
				p = t;
				t = (q+p+1) / 2;
			} else {
				q = t;
				t = (q+p) / 2;
			}
			c++;
		}
		cout << c << endl;
		
		free(a);
	}
	
	return 0;
}

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

int main(){
  int l,r,m,n,a[100],g,c;
  for(;;){
    cin >> n;
    if(!n)break;
    for(int i=0;i<n;i++)cin >> a[i];
    cin >> g;


    l = 0;r = n-1;
    c = 0;
    while(l<=r){
      c++;
      m = (l+r)/2;
      if(a[m]==g)break;
      else if(a[m]>g)r = m-1;
      else l = m+1;
    }

    cout << c << endl;
  }
}

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

using namespace std;

#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define ELEM(array) (sizeof (array)/sizeof *(array))
#define MAX_N 100
typedef long long ll;

int n = -1;
int a[101];
int k;

void serch(int k)
{
	
	int ans = 1;

	int lb = 0; int ub = n-1;
	while (ub - lb>1)
	{
		int mid = (lb + ub) / 2;
		if (a[mid] == k) {
			ans++; break;
		}
		else if (a[mid] > k){
			ub = mid;
			ans++;
		}
		else {
			lb = mid;
			ans++;
		}
	}
	cout <<"ans=" <<ans << endl;
	
	return;
}
int main()
{
	while (cin>>n&& n!=0)
	{
		
		REP(i, n){
			cin >> a[i];
		}
		
		cin >> k;
		serch(k);
		

	}


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