数据结构实验之查找六:顺序查找

来源:互联网 发布:i代表什么矩阵 编辑:程序博客网 时间:2024/05/13 18:57

数据结构实验之查找六:顺序查找

Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic

Problem Description

在一个给定的无序序列里,查找与给定关键字相同的元素,若存在则输出找到的元素在序列中的位序和需要进行的比较次数,不存在则输出"No",序列位序从1到n,要求查找从最后一个元素开始,序列中无重复元素。

Input

连续多组数据输入,每组输入数据第一行首先输入两个整数n(n <= 1000000)和k,n是数组长度,k是待查找的关键字,然后连续输入n个整数,数据间以空格间隔。

Output

若存在则输出元素在序列中的位序和比较次数,不存在则输出No。

Example Input

5 94 6 8 9 137 4-1 3 2 5 4 6 9 20 904 6 8 9 13 17 51 52 54 59 62 66 76 78 80 85 88 17 20 21

Example Output

4 25 3No

#include <iostream>#include <cstring>using namespace std;int a[1000010];int n,k;int sea(int k){    int i;    a[0]=k;    for(i=n;a[i]!=k;i--);    return i;}int main(){    while(cin>>n>>k)    {        for(int i=1;i<=n;i++)        {            cin>>a[i];        }        int x=sea(k);        if(x)            cout<<x<<" "<<n-x+1<<endl;        else            cout<<"No"<<endl;    }    return 0;}


0 0
原创粉丝点击