数据结构上机实验之顺序查找

来源:互联网 发布:淘宝淡季是几月份 编辑:程序博客网 时间:2024/05/16 00:26

数据结构上机实验之顺序查找

Time Limit: 1000MS Memory limit: 65536K

题目描述

 在一个的序列里,查找元素是否存在,若存在输出YES,不存在输出NO.

输入

 本题多组数据,首先输入一个数字n,然后输入n(n<=1000)个数,然后再输入一个查找数字。

输出

 若存在输出YES,不存在输出NO.

示例输入

41 3 5 83

示例输出

YES

提示

 

来源

 cz




#include<bits/stdc++.h>using  namespace std;int finded(int a[],int l,int r,int key){    int low=l,high=r,mid;    if(l<=r)    {        mid=(high+low)/2;        if(a[mid]==key)        {            return mid;        }        if(a[mid]>key)        {            return finded(a,l,mid-1,key);        }        else        {            return finded(a,mid+1,r,key);        }    }    else        return 0;}int comp(const void *a,const void *b){    return *(int *)a-*(int *)b;}int main(){    int n,key,a[1200];    while(cin>>n)    {        for(int i=0;i<n;i++)        {            cin>>a[i];        }        cin>>key;        qsort(a,n,sizeof(int),comp);        if(finded(a,0,n,key))        {            cout<<"YES"<<endl;        }        else        {            cout<<"NO"<<endl;        }    }    return 0;}


0 0