-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_search.cpp
More file actions
43 lines (38 loc) · 978 Bytes
/
linear_search.cpp
File metadata and controls
43 lines (38 loc) · 978 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <bits/stdc++.h>
// 番兵を追加してwhile文で回す方が
// 二重for文で回すより若干速い
bool LinearSearch(int num, int A[], int n) {
// 終了判定用の値を配列にセットする
// →番兵
// while文で必ずループ終了する事が保証されているので
// 比較演算がfor文より一個少なくて済む
A[n] = n;
int i=0;
while (A[i] != num) {
// 末端まで見つからなかった
if (i == n) return false;
i++;
}
return true;
}
int main()
{
static const int MAX = 20000;
static const int MAX2 = 600;
int n, s, R[MAX], S[MAX2],tmp;
int cnt = 0;
std::cin >> n;
for (int i=0; i<n; i++) {
std::cin >> R[i];
}
std::cin >> s;
for (int i = 0; i < s; i++)
{
std::cin >> S[i];
}
for (int m=0; m<s; m++) {
if (LinearSearch(S[m], R, n)) cnt++;
}
std::cout << cnt << "\n";
return 0;
}