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<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while(cin>>n,n){
        int num[n],ans;
        for(int i=0;i<n;i++)cin>>num[i];
        cin>>ans;
        int serch,kawa[2]={0,n-1};
        for(int i=1;;i++){
            serch=(kawa[0]+kawa[1])/2;
            if(num[serch]==ans||kawa[1]-kawa[0]==0){
                cout<<i<<endl;
                break;
            }
            if(kawa[1]-kawa[0]==1){
                cout<<i+1<<endl;
                break;
            }
            if(num[serch]>ans)kawa[1]=serch-1;
            else kawa[0]=serch+1;
        }
    }
}

C++: #include <cstdio>

int n,k;
int a[100001];

int main(void){
	while(1){
		scanf("%d",&n);
		if(n==0)break;
		for(int i=0;i<n;i++){
			scanf("%d",&a[i]);
		}
		scanf("%d",&k);
		int l=0,r=n-1,cnt=0;
		while(1){
			cnt++;
			int m=(l+r)/2;
			//printf("%d %d %d %d %d %d\n",l,m,r,a[l],a[m],a[r]);
			if(a[m]==k || r-l<=0)break;
			if(a[m]<k)l=m+1;
			if(a[m]>k)r=m-1;
		}
		printf("%d\n",cnt);
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)

int n,a[100],t;

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

C++: #include<stdio.h>

int main(void)
{
	int x[100];
	int st,fn,a,n,i,ty,kai;
	while(scanf("%d",&n)!=EOF){
		kai=1;  st=0; fn=n-1;
		for(i=0;i<n;i++) scanf("%d",&x[i]);
		scanf("%d",&a);
		while(0==0){
			ty=(st+fn)/2;
			if(a==x[ty]||st==fn)break;
			else if(a>x[ty])  st=ty+1;
			else fn= ty-1;
			kai++;
		}
		printf("%d\n",kai);
	}
	return 0;
}

C++: #include<stdio.h>

int main(void)
{
	int x[100];
	int st,fn,a,n,i,j,kai;
	while(0==0){
		scanf("%d",&n);
		if(n==0)break;		
		kai=1;  st=0; fn=n-1;
		for(i=0;i<n;i++) scanf("%d",&x[i]);
		scanf("%d",&a);
		while(0==0){
			j=(st+fn)/2;
			if(a==x[j]||st==fn)break;
			else if(a>x[j])  st=j+1;
			else fn= j-1;
			kai++;
		}
		printf("%d\n",kai);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repb(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

signed main() {
	int n, x;
	while (1) {
		cin >> n;
		if (n == 0) break;
		vi d(n);
		rep(i, 0, n) {
			cin >> d[i];
		}
		cin >> x;
		int cnt = 0, l = 0, r = n - 1;
		while (l<=r) {
			cnt++;
			int mid = (l + r) / 2;
			if (d[mid] > x) {
				r = mid - 1;
			}
			else if (d[mid] < x) {
				l = mid + 1;
			}
			else{
				break;
			}
		}
		cout << cnt << endl;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while(cin>>n,n){
        int num[n],ans;
        for(int i=0;i<n;i++)cin>>num[i];
        cin>>ans;
        int serch,kawa[2]={0,n-1};
        for(int i=1;kawa[1]>=kawa[0];i++){
            serch=(kawa[0]+kawa[1])/2;
            if(num[serch]>ans)kawa[1]=serch-1;
            else if(num[serch]<ans)kawa[0]=serch+1;
            if(num[serch]==ans||kawa[0]>kawa[1]){
                cout<<i<<endl;
                break;
            }
        }
    }
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

int main(){
    while(true){
	int n;
	cin >> n;
	vector<int> a(n);
	if(n == 0) break;
	REP(i,n) cin >> a[i];
	int num;
	cin >> num;
	int l,m,h,ans;
	h = n-1;
	l = 0;
	m = (h+l)/2;
	ans = 0;
	while(l <= h){
	    if(a[m] > num){
		h = m - 1;
	    }else if (a[m] < num){
		l = m + 1;
	    }else{
		ans++;
		break;
	    }
	    m = (h+l)/2;
	    ans++;
	}
	cout << ans << endl;
    }
    return 0;
}

C++: #include <cstdio>

int n,k;
int a[200];

int main(void){
	while(1){
		scanf("%d",&n);
		if(n==0)break;
		for(int i=0;i<n;i++){
			scanf("%d",&a[i]);
		}
		scanf("%d",&k);
		int l=0,r=n,cnt=0;
		while(1){
			cnt++;
			int m=(l+r-1)/2;
			//printf("%d %d %d %d %d %d\n",l,m,r,a[l],a[m],a[r]);
			if(a[m]==k || r-l<=1)break;
			if(a[m]<k)l=m+1;
			if(a[m]>k)r=m-1;
		}
		printf("%d\n",cnt);
	}
	return 0;
}

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

using namespace std;

int main() {
	int n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		vector<long long int> data(n);
		int left = 0;
		int right = n-1;
		for (int i = 0; i < n; i++) {
			cin >> data[i];
		}
		int counter = 0;
		int tansa;
		cin >> tansa;
		while (true) {
			int half = (left+right) / 2;
			counter++;
			if (left == right) {
				break;
			}
			if (data[half] == tansa) {
				break;
			} else if (data[half] < tansa) {
				left = half+1;
			} else {
				right = half-1;
			}
			if (left > right) {
				break;
			}
		}
		cout << counter << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<unordered_map>
#include<queue>
#include<vector>
using namespace std;

//#define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#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 all(a) (a).begin(),(a).end()
#define dump(o) {cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) {cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

signed main() {
	for (int n; cin >> n&&n;) {
		vector<int> a(n);
		rep(i, 0, n)cin >> a[i];
		int s; cin >> s;
		int l = 0, r = n - 1;
		int cnt = 0;
		while (true) {
			cnt++;
			if (r - l == 0)break;
			int m = (l + r) / 2;
			//dump(m); dump(l); dump(r);
			if (a[m] > s)r = m - 1;
			else if (a[m] < s)l = m + 1;
		}
		cout << cnt << endl;
	}
	return 0;
}

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

using namespace std;

int main(){

	int n;
	while(cin>>n&&n!=0){
		vector<int> nums;
		int t;
		for(int i = 0; i < n; i++){
			cin>>t;
			nums.push_back(t);
		}
		int s;
		cin>>s;
		int ub=n-1;
		int lb=0;
		int cnt=0;
		while(1){
			int mid=ub+lb;
			mid/=2;
			cnt++;
			if(nums[mid]==s||ub==lb){
				break;
			}
			else if(nums[mid]>s){
				ub=mid-1;
			}
			else if(nums[mid]<s){
				lb=mid+1;
			}
		}
		cout<<cnt<<endl;
	}

	return 0;
}