元素查找

来源:互联网 发布:ios和mac 编辑:程序博客网 时间:2024/05/16 12:34

http://codevs.cn/problem/1230/
输入一串数,输入查询,看这个数否存在。
题很假单。我第一个想法是桶排。看了下标题,是哈希表,以及线性结构,然后自己自从学过哈希表从来就没写过,写一个练练吧。
从线性表角度看,是应优先队列,貌似如果是数组,再加一个快排,应该也是稳稳的。

#include<iostream>#include<stdio.h>#include<cmath>#include<algorithm>#include<stack>#include<string>using namespace std;int a[100005] = { 0 };int main(){    int n, k;    cin >> n >> k;    int x;    for (int i = 0; i < n; i++)    {        scanf_s("%d",&x);        int weizhi = x % 100005;        while (a[weizhi]!=x)        {            if (!a[weizhi])                a[weizhi] = x;            else            {                weizhi++;                if (weizhi == 100005)                {                    weizhi = 0;                }            }        }    }    for (int i = 0; i < k; i++)    {        scanf_s("%d",&x);        int weizhi = x % 100005;        int biaozhi = 1;        while (a[weizhi] != x)        {            if (!a[weizhi])            {                biaozhi = 0;                break;            }            else            {                weizhi++;                if (weizhi == 100005)                {                    weizhi = 0;                }            }        }        if (biaozhi)            printf("YES\n");        else            printf("NO\n");    }    system("pause");    return 0;}
0 0
原创粉丝点击