日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

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

發布時間:2023/11/27 生活经验 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 顺序表应用6:有序顺序表查询 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

順序表應用6:有序順序表查詢

Time Limit:?7MS?Memory Limit:?700KB Submit?Statistic

Problem Description

順序表內按照由小到大的次序存放著n個互不相同的整數(1<=n<=20000),任意輸入一個整數,判斷該整數在順序表中是否存在。如果在順序表中存在該整數,輸出其在表中的序號;否則輸出“No Found!"。

Input

第一行輸入整數n,表示順序表的元素個數;
第二行依次輸入n個各不相同的有序整數,代表表里的元素;
第三行輸入整數t,代表要查詢的次數;
第四行依次輸入t個整數,代表每次要查詢的數值。

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 <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <malloc.h>
#define LISTINCREASMENT 20012 ? ? ? ? ? ? ? ? /*每次分配元素的個數*/
#define ?LISTSIZE 20012 ? ? ? ? ? ? ? ? ? ? ? ? /*順序存儲的最大個數*/
#define ?OVERFLOW -1
#define ?OK 1
int n,m;
using namespace std;
typedef int ElemType;


typedef struct ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? /*順序表元素的的定義*/
{
? ? ElemType * elem;
? ? int length;
? ? int listsize;
} Sqlist;


int SqInitial(Sqlist &L) ? ? ? ? ? ? ? ? ? ? ? ? ? /*初始化線性表*/
{
? ? L.elem=(ElemType *) malloc (LISTSIZE*sizeof(ElemType));
? ? if (! L.elem) ?exit(OVERFLOW); //存儲分配失敗
? ? L.length=0;
? ? L.listsize=LISTSIZE;
? ? return OK;
}


int ListInsert(Sqlist &L,int i,ElemType e) ? ? ? ? ? ?/*插入元素*/
{
? ? if(i<1|| i > L.length+1) exit(-1);
? ? if(L.length>=L.listsize)
? ? {
? ? ? ? ElemType*newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREASMENT)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *sizeof(ElemType));
? ? ? ? if(!newbase) ? return ?OVERFLOW;// 當前存儲空間已滿


L.elem=newbase;
? ? ? ? L.listsize+=LISTINCREASMENT; ? ? ? ? /*表的容量不足分配內存*/
? ? }
? ? ElemType * ?q=&(L.elem[i-1]);
? ? ElemType * ?p;
? ? for(p=&(L.elem[L.length-1]); p>=q; --p)
? ? ? ? *(p+1)=*p;
? ? *q=e;
? ? ++L.length;
? ? return OK;


}
void display(Sqlist &L)
{
? ? int i;
? ? for(i=0;i<L.length-1;i++)
? ? {
? ? ? ? cout<<L.elem[i]<<" ";
? ? }
? ? ?cout<<L.elem[i]<<endl;
}




int query(Sqlist &L,int i,int j,int key)
{
? ? int mid;
? ? while(i<=j)
? ? {
? ? ? ? mid = (j+i)/2;
? ? ? ? /*cout<<"L.elem[mid]=="<<L.elem[mid]<<endl;
? ? ? ? cout<<"L.elem[i]=="<<L.elem[i]<<endl;
? ? ? ? cout<<"L.elem[j]=="<<L.elem[j]<<endl;
? ? ? ? */
? ? ? ? if(L.elem[mid]==key)
? ? ? ? ? ? return mid+1;
? ? ? ? if(L.elem[mid]<key)
? ? ? ? {
? ? ? ? ? ? i = mid+1;
? ? ? ? }
? ? ? ? if(L.elem[mid]>key)
? ? ? ? {
? ? ? ? ? ? j = mid-1;
? ? ? ? }
? ? }
? ? return -1;
}




int main()
{
? ? Sqlist L,L1,L2;
? ? int t =1 ,d;
? ? cin>>n;
? ? SqInitial(L);
? ? //printf("構建長度為len的順序表。\n");
? ? for(t=1; t<=n; t++) ? ? ? ? ? ? ? ? ? ? ? ? /*構建長度為n的順序表*/
? ? {
? ? ? ? //printf("Please input the %dth list elem:",t);
? ? ? ? scanf("%d",&d);
? ? ? ? ListInsert(L,t,d);
? ? }
? ? cin>>m;
? ? while(m--)
? ? {
? ? ? ? scanf("%d",&t);
? ? ? ? d = query(L,0,n-1,t);
? ? ? ? if(d==-1)
? ? ? ? ? ? cout<<"No Found!"<<endl;
? ? ? ? else
? ? ? ? ? ? cout<<d<<endl;


? ? }






return 0;
}

轉載于:https://www.cnblogs.com/CCCrunner/p/6444613.html

總結

以上是生活随笔為你收集整理的顺序表应用6:有序顺序表查询的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。