數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)之查找六:順序查找
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
在一個(gè)給定的無序序列里,查找與給定關(guān)鍵字相同的元素,若存在則輸出找到的元素在序列中的位序和需要進(jìn)行的比較次數(shù),不存在則輸出"No",序列位序從1到n,要求查找從最后一個(gè)元素開始,序列中無重復(fù)元素。
Input
連續(xù)多組數(shù)據(jù)輸入,每組輸入數(shù)據(jù)第一行首先輸入兩個(gè)整數(shù) n (n <= 10^6) 和 k (1 <= k <= 10^7),n是數(shù)組長度,k是待查找的關(guān)鍵字,然后連續(xù)輸入n個(gè)整數(shù) ai (1 <= ai <= 10^7),數(shù)據(jù)間以空格間隔。
Output
若存在則輸出元素在序列中的位序和比較次數(shù),不存在則輸出No。
Example Input
5 9
4 6 8 9 13
7 4
-1 3 2 5 4 6 9
20 90
4 6 8 9 13 17 51 52 54 59 62 66 76 78 80 85 88 17 20 21
Example Output
4 2
5 3
No
Hint
本題數(shù)據(jù)量較大,如果你使用 C++ 的 cin 讀入,建議在 main 函數(shù)開頭加入一行ios::sync_with_stdio(false);
以防止讀入超時(shí)。
/*
Name
Author:Mr.z
Time:2016-12-12
*/
#include <iostream>
#include <string.h>
using namespace std;
int hash[1011],p,ADDR[1100000],n;
int main(){
int n,k,i;
while(cin >> n >> k){
int count=0;
for(int i=0;i<n;i++)
cin >> ADDR[i];
for(i=n-1;i>=0;i--){
if(k==ADDR[i])
break;
else count ++;
}
if(i==-1) cout << "No\n";
else cout << i+1 << " " << count+1 << endl;
}
}
/***************************************************
User name: zhxw150244李政
Result: Accepted
Take time: 64ms
Take Memory: 544KB
Submit time: 2016-12-12 16:56:15
****************************************************/