Think:
哇, 实验室 外网贼慢贼慢的了~~ 但又必须完成100博客的 flag 就自己发邮箱了。
这题 其实就是 查找 元素是否存在就可以了, 因为 数据较大, 所以 我采用 二分查找的 方法来解题, 可以大大降低 时间复杂度
Problem Description
顺序表内按照由小到大的次序存放着n个互不相同的整数,任意输入一个整数,判断该整数在顺序表中是否存在。如果在顺序表中存在该整数,输出其在表中的序号;否则输出“No Found!”。
Input
第一行输入整数n (1 <= n <= 100000),表示顺序表的元素个数;
第二行依次输入n个各不相同的有序非负整数,代表表里的元素;
第三行输入整数t (1 <= t <= 100000),代表要查询的次数;
第四行依次输入t个非负整数,代表每次要查询的数值。
保证所有输入的数都在 int 范围内。
Output
输出t行,代表t次查询的结果,如果找到在本行输出该元素在表中的位置,否则本行输出No Found!
Example Input
10
1 22 33 55 63 70 74 79 80 87
4
55 10 2 87
Example Output
4
No Found!
No Found!
10
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f
typedef int Elemtype;
typedef struct
{
Elemtype *elem;
int length;
int listsize;
} seqlist;
void Mal(seqlist &L);
int Bsearch(int x, int y, seqlist &L, int key);
void Input(seqlist &L, int len);
//void Output(seqlist &L);
int main()
{
// seqlist L;
int m, n, T;
// cin >> T;
// while(T --)
// {
seqlist L;
cin >> n;
Mal(L);
Input(L, n);
// output(L);
cin >> m;
while(m --)
{
int key;
scanf("%d",&key);
// int N = L.length;
int cnt = Bsearch(0, L.length, L, key);
if (cnt == -1)
cout << "No Found!" << endl;
else
cout << cnt + 1 << endl;
}
// }
return 0;
}
void Mal(seqlist &L)
{
L.elem = (Elemtype *)malloc(INF * sizeof(Elemtype));
L.length = 0;
}
int Bsearch(int x, int y, seqlist &L, int key)
{
int left = x;
int right = y;
int mid;
if (left <= right)
{
mid = (left + right) / 2;
if (key == L.elem[mid])
return mid;
if (key < L.elem[mid])
return Bsearch(left, mid - 1, L, key);
if (key > L.elem[mid])
return Bsearch(mid + 1, right, L, key);
}
else
return -1;
return 0;
}
void Input(seqlist &L, int len)
{
L.listsize = len;
int i;
for(i = 0;i < len;i ++)
{
scanf("%d",&L.elem[L.length ++]);
}
}