您的位置:首页 > 其它

顺序表应用6:有序顺序表查询

2018-03-17 12:16 162 查看
顺序表应用6:有序顺序表查询

Time Limit: 1000 ms Memory Limit: 4096 KiB

Submit Statistic

Problem Description

顺序表内按照由小到大的次序存放着n个互不相同的整数,任意输入一个整数,判断该整数在顺序表中是否存在。如果在顺序表中存在该整数,输出其在表中的序号;否则输出“No Found!”。

Input

第一行输入整数n (1 <= n <= 100000),表示顺序表的元素个数;

第二行依次输入n个各不相同的有序非负整数,代表表里的元素;

第三行输入整数t (1 <= t <= 100000),代表要查询的次数;

第四行依次输入t个非负整数,代表每次要查询的数值。

保证所有输入的数都在 int 范围内。

Output

输出t行,代表t次查询的结果,如果找到在本行输出该元素在表中的位置,否则本行输出No Found!

Sample Input

10

1 22 33 55 63 70 74 79 80 87

4

55 10 2 87

Sample Output

4

No Found!

No Found!

10

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define max 100010

typedef int ElemType;

typedef struct
{
ElemType *elem;
int length;
int listsize;
}SqList;

void InitSqList(SqList &L)
{
L.elem=(ElemType*)malloc(max*sizeof(ElemType));
L.length = 0;
L.listsize = max;
}
void List_insert(SqList &L, int len)
{
int i;
for(i=1; i<=len; i++)
{
scanf("%d", &L.elem[i]);
}
L.length = len;
}
void List_h(SqList &L, int n, int e)
{
int l=1, mid;
while(l <= n)
{
mid = (l + n) / 2;
if(L.elem[mid] == e)
{
printf("%d\n", mid);
return ;
}
else if(L.elem[mid] > e)
{
n = mid - 1;
}
else l = mid + 1;
}
printf("No Found!\n");
}
int main()
{
int n, t;
scanf("%d", &n);
SqList L;
InitSqList(L);
List_insert(L, n);
scanf("%d", &t);
while(t--)
{
int e;
scanf("%d", &e);
List_h(L, n, e);
}
return 0;
}

/***************************************************
User name: jk160532姜兴友
Result: Accepted
Take time: 64ms
Take Memory: 496KB
Submit time: 2017-09-21 16:10:50
****************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: