#include <bits/stdc++.h>
#define Max 1010000
using namespace std;
typedef int ElemType;
class List
{
public:
ElemType *elem;
int maxSize;
int size;
List() /*List类的无参构造函数*/
{
elem = (ElemType *)malloc(Max * sizeof(ElemType));
maxSize = Max;
size = 0;
}
void CreatList(int n) /*建表*/
{
int x;
for(int i = 0; i < n; i++)//这建表下标从1开始,否则的话你返回的0是找到的第一个还是没找到????
{
cin >> x;
elem[size++] = x;
}
}
void MergeList(List a, List b)
{
int i,j;
size = 0;
i = 0;
j = 0;
while(i < a.size && j < b.size)
{
if(a.elem[i] < b.elem[j])
{
elem[size++] = a.elem[i];
i++;
}
else
{
elem[size++] = b.elem[j];
j++;
}
}
while(i < a.size)
{
elem[size++] = a.elem[i];
i++;
}
while(j < b.size)
{
elem[size++] = b.elem[j];
j++;
}
}
int BinaryFind(ElemType elem[], int l, int r, int key)
{
int i;
if(l > r)
return false;
i = (l + r) / 2;
if(elem[i] == key)
return i+1;
if(key < elem[i])
return BinaryFind(elem, l, i-1, key);
if(key > elem[i])
return BinaryFind(elem, i+1, r, key);
return 0;
}
void OutputList() /*输出*/
{
for(int i = 0; i < size; i++)
i == size - 1? cout << elem[i] << endl : cout << elem[i] << " ";
}
void DestroyList() /*销毁顺序表*/
{
delete elem;
}
};
int main()
{
List L;
int n, t, key;
cin >> n;
L.CreatList(n);
cin >> t;
while(t--)
{
int f;
cin >> key;
f = L.BinaryFind(L.elem, 0, n-1, key);
if(f)
cout << f << endl;
else
cout << "No Found!" << endl;
}
return 0;
}
有序顺序表查询
最新推荐文章于 2020-06-10 21:26:37 发布