有序表的折半查找
#include <iostream>
using namespace std;
typedef struct
{
int r[100];
int length;
}table;
int create(table &t)
{
int a;
t.length=0;
cin>>a;
while(a!=0)
{
t.r[t.length]=a;
t.length++;
cin>>a;
}
return 1;
}
int search_bin(table &t,int key)
{
int low,high,mid;
low=1;high=t.length;
while(low<=high)
{
mid=(low+high)/2;
if(key==t.r[mid]) return mid;
else if(key<t.r[mid]) high=mid-1;
else low=mid+1;
}
return 0;
}
int main()
{
table t;
int key,c;
create(t);
cin>>key;
c=search_bin(t,key);
cout<<c+1;
return 0;
}