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)
	}
}

Python: import sys
readline = sys.stdin.readline
write = sys.stdout.write

def solve():
    N = int(input())
    if N == 0:
        return False
    A = [int(readline()) for i in range(N)]
    k = int(readline())

    ans = 0
    left = 0; right = N-1
    while left <= right:
        mid = (left + right) >> 1
        v = A[mid]
        ans += 1
        if v == k:
            break
        if v < k:
            left = mid+1
        else:
            right = mid-1
    write("%d\n" % ans)
    return True
while solve():
    ...

Python: # AOJ 0177 Distance Between Two Cities
# Python3 2018.6.20 bal4u

while True:
	n = int(input())
	if n == 0: break
	a = [0]*120
	for i in range(n): a[i] = int(input())
	k = int(input())
	cnt = 0
	l, r = 0, n-1
	while l <= r:
		cnt += 1
		m = (l + r) >> 1
		if k == a[m]: break
		if k < a[m]: r = m-1
		else: l = m+1
	print(cnt)

Python: def search(n,d,t):
    b = 0
    e = n
    r = 0
    while True:
        r += 1
        if b > e:
            return r
        else:
            i = (b+e)//2
            if t == d[i]:
                return r
            elif t < d[i]:
                e = i - 1
            else:
                b = i + 1
while True:
    n = int(input())
    if n == 0:
        break
    d = [int(input()) for i in range(n)]
    t = int(input())

    m = search(n,d,t)
    print(m)

Python: while 1:
    l,r=0,int(input())
    a=[int(input()) for _ in range(r)]
    r-=1;c=0
    k=int(input())
    while l<=r:
        c+=1
        m=(l+r)//2
        if a[m]==k:break
        if a[m]<k:l=m+1
        else:r=m-1
    print(c)

Python: # -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0188

"""
import sys
from sys import stdin
input = stdin.readline


def b_search(data, k):
    count = 0
    l = 0
    r = len(data) - 1

    while r >= l:
        count += 1
        mid = (l + r) // 2
        if data[mid] == k:
            break
        if data[mid] > k:
            r = mid - 1
        else:
            l = mid + 1
    return count



def main(args):
    while True:
        n = int(input())
        if n == 0:
            break
        data = [int(input()) for _ in range(n)]
        k = int(input())
        result = b_search(data, k)
        print(result)



if __name__ == '__main__':
    main(sys.argv[1:])

Python: def search(data, key):
    cnt = 0
    left, right = -1, len(data)

    while right - left > 1:
        mid = (left + right) // 2
        cnt += 1
        if data[mid] == key: break
        elif data[mid] > key:
            right = mid
        else:
            left = mid

    return cnt

def solve():
    N = int(input())
    if N == 0: return -1
    data = [int(input()) for i in range(N)]
    k = int(input())
    return search(data, k)

while True:
    ans = solve()
    if ans == -1: break
    print(ans)

Python: while True:
    n = int(input())
    if n == 0: break
    a = [int(input()) for _ in range(n)]
    k = int(input())
    l, r, c = 0, n-1, 0
    while l <= r:
        c += 1
        m = (l+r) >> 1
        if k == a[m]:
            break
        elif k < a[m]:
            r = m-1
        else:
            l = m+1
    print(c)

Python: def nibutan(f_id,e_id,f_num,count):
    
    
    m_id = int((f_id + e_id) / 2)
    m_num = n_lis[m_id]

    count += 1
    if (f_id > e_id):
        return count
    else:

        if m_num > f_num:
            return(nibutan(f_id,m_id -1,f_num,count))
        elif m_num < f_num:
            return(nibutan(m_id + 1,e_id,f_num,count))
        else:
            return count
                
while True:
    global n_lis

    n = int(input())
    if n == 0:
        break
    n_lis = []
    for i in range(n):
        n_lis.append(int(input()))
    f_n = int(input())
    ans = nibutan(0,n,f_n,0)
    print(ans)

Python: def binary_search(num_list, num):
    left = 0
    right = len(num_list) - 1

    count = 0

    while left <= right:
        mid = left + (right - left) // 2
        count += 1

        if num_list[mid] == num:
            break
        elif num_list[mid] < num:
            left = mid + 1
        else:
            right = mid - 1

    return count


while True:

    input_count = int(input())

    if input_count == 0:
        break

    num_list = [int(input()) for _ in range(input_count)]

    search_number = int(input())

    result = binary_search(num_list, search_number)

    print(result)

Python: def search(n,d,t):
    b = 0
    e = n
    r = 0
    while True:
        r += 1
        if b > e:
            return r
        else:
            i = (b+e)//2
            if t == d[i]:
                return r
            elif t < d[i]:
                e = i - 1
            else:
                b = i + 1
while True:
    n = int(input())
    if n == 0:
        break
    d = [int(input()) for i in range(n)]
    t = int(input())

    m = search(n,d,t)
    print(m)

Python: def search_num(k, alst):
  left = 0
  right = len(alst) - 1
  cnt = 0
  while left <= right:
    mid = (left + right) // 2
    cnt += 1
    if alst[mid] == k:
      return cnt
    if alst[mid] < k:
      left = mid + 1
    else:
      right = mid - 1
  return cnt

while True:
  n = int(input())
  if n == 0:
    break
  alst = [int(input()) for _ in range(n)]
  k = int(input())
  print(search_num(k, alst))

Python: # -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0188

"""
import sys
from sys import stdin
input = stdin.readline


def b_search(data, k):
    count = 0
    l = 0
    r = len(data) - 1

    while True:
        count += 1
        mid = (l + r) // 2
        if (data[mid] == k) or (l == r):
            break
        if data[mid] > k:
            r = mid
        else:
            l = mid + 1
    return count



def main(args):
    while True:
        n = int(input())
        if n == 0:
            break
        data = [int(input()) for _ in range(n)]
        k = int(input())
        result = b_search(data, k)
        print(result)



if __name__ == '__main__':
    main(sys.argv[1:])

Python: def nibutan(f_id,e_id,f_num,count):
    
    
    m_id = int((f_id + e_id) / 2)
    m_num = n_lis[m_id]

   
    if (f_id > e_id):
        return count
    else:
        count += 1

        if m_num > f_num:
            return(nibutan(f_id,m_id -1,f_num,count))
        elif m_num < f_num:
            return(nibutan(m_id + 1,e_id,f_num,count))
        else:
            return count
                
while True:
    global n_lis

    n = int(input())
    if n == 0:
        break
    n_lis = []
    for i in range(n):
        n_lis.append(int(input()))
    f_n = int(input())
    ans = nibutan(0,n - 1,f_n,0)
    print(ans)

Python: # Aizu Problem 0188: Search
#
import sys, math, os, bisect

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


def search(n, A, s):
    cnt = 0
    left, right = 0, n - 1
    while left <= right:
        mid = (left + right) // 2
        cnt += 1
        if A[mid] == s:
            return cnt
        elif A[mid] > s:
            right = mid - 1
        else:
            left = mid + 1
    return cnt

while True:
    n = int(input())
    if n == 0:
        break
    A = [int(input()) for _ in range(n)]
    s = int(input())
    print(search(n, A, s))