#include<iostream> using namespace std; #define N 10 int A[N+1]= {0,-1,89,72,10,-17,20,8,789,45,10}; //A[1]~A[10] typedef struct { //查找表的数据结构 int *elem; //元素存储空间基址,建表时按实际长度分配, 0 号单元留空 int TableLen; //表的长度 } SeqList; int Binary_Search (SeqList L, int key) { int low=0,high=L.TableLen-1, mid ; while (low<=high ) { mid=(low+high)/2 ; //取中间位置 if (L.elem[mid]==key ) return mid; //查找成功则返回所在位置 else if(L.elem[mid]>key) high=mid-1 ;//从前半部分继续查找 else low=mid+1 ;//从后半部分继续查找 } return -1 ; //查找失败,返回- 1 } int main() { SeqList T; T.elem=A; T.TableLen=N; cout<<T.elem[10]; cout<<Binary_Search(T,10); return 0; }