顺序表应用6:有序顺序表查询
来源:互联网 发布:数据魔方是什么 编辑:程序博客网 时间:2024/05/17 20:13
Problem Description
顺序表内按照由小到大的次序存放着n个互不相同的整数,任意输入一个整数,判断该整数在顺序表中是否存在。如果在顺序表中存在该整数,输出其在表中的序号;否则输出“No Found!"。
Input
第一行输入整数n (1 <= n <= 100000),表示顺序表的元素个数; 第二行依次输入n个各不相同的有序非负整数,代表表里的元素; 第三行输入整数t (1 <= t <= 100000),代表要查询的次数; 第四行依次输入t个非负整数,代表每次要查询的数值。
保证所有输入的数都在 int 范围内。
Output
输出t行,代表t次查询的结果,如果找到在本行输出该元素在表中的位置,否则本行输出No Found!
Example Input
101 22 33 55 63 70 74 79 80 87455 10 2 87Example Output
4No Found!No Found!10Hint
Author
#include <stdio.h>#include <stdlib.h>#define Max 201000using namespace std;typedef int elemtype;int len;typedef struct{ elemtype *elem; int listsize; int length;}List;void creat(List &L, int len){ L.elem = new elemtype[Max]; L.length = len; L.listsize = Max;}void output(List &L){ int i; for(i = 0; i < L.length - 1; i++) { printf("%d ", L.elem[i]); } printf("%d\n", L.elem[L.length - 1]);}void input(List &L){ int i; for(i = 1; i <= L.length; i++) { scanf("%d", &L.elem[i]); }}int locate(List &L, int low, int high, int key){ int mid; if(low <= high) { mid = (low + high) / 2; if(L.elem[mid] == key)return mid; else if(L.elem[mid] > key)return locate(L, low, mid - 1, key); else return locate(L, mid + 1, high, key); } else return -1;}int main(){ List L; int n, t, key, ans; scanf("%d", &n); creat(L, n); input(L); scanf("%d", &t); while(t--) { scanf("%d", &key); ans = locate(L, 1, n, key); if(ans == -1)printf("No Found!\n"); else printf("%d\n", ans); } return 0;}
阅读全文
0 0
- SDUT 3330顺序表应用6:有序顺序表查询
- 顺序表应用6:有序顺序表查询
- 顺序表应用6:有序顺序表查询
- 顺序表应用6:有序顺序表查询
- 顺序表应用6:有序顺序表查询
- 顺序表应用6:有序顺序表查询
- 【3330】顺序表应用6:有序顺序表查询
- 顺序表应用6:有序顺序表查询
- 顺序表应用6:有序顺序表查询
- 顺序表应用6:有序顺序表查询
- 顺序表应用6:有序顺序表查询
- 顺序表应用6:有序顺序表查询
- 顺序表应用6:有序顺序表查询
- 顺序表应用6:有序顺序表查询
- sdut oj3330 顺序表应用6:有序顺序表查询
- 顺序表应用6:有序顺序表查询
- 顺序表应用6:有序顺序表查询
- SDUT3330顺序表应用6:有序顺序表查询
- GeekBand笔记-《C++设计模式》 第三周
- firewall,iptablse和selinux防火墙
- 并查集题集
- (M)Backtracking:526. Beautiful Arrangement
- Windows 上好用的7款下载工具
- 顺序表应用6:有序顺序表查询
- openssh-server
- 多媒体之使用PlaySound播放音频
- 重拾Java--功底篇之HashMap
- 第二章 Linux安装课后习题
- 线段树题集
- 个人总结8
- 纪念重学C/C++,Linux的日子
- Linux网络管理