forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1.cpp
34 lines (28 loc) Β· 1.08 KB
/
1.cpp
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
#include <bits/stdc++.h>
using namespace std;
// μμ°¨ νμ μμ€μ½λ ꡬν
int sequantialSearch(int n, string target, vector<string> arr) {
// κ° μμλ₯Ό νλμ© νμΈνλ©°
for (int i = 0; i < n; i++) {
// νμ¬μ μμκ° μ°Ύκ³ μ νλ μμμ λμΌν κ²½μ°
if (arr[i] == target) {
return i + 1; // νμ¬μ μμΉ λ°ν (μΈλ±μ€λ 0λΆν° μμνλ―λ‘ 1 λνκΈ°)
}
}
return -1; // μμλ₯Ό μ°Ύμ§ λͺ»ν κ²½μ° -1 λ°ν
}
int n; // μμμ κ°μ
string target; // μ°Ύκ³ μ νλ λ¬Έμμ΄
vector<string> arr;
int main(void) {
cout << "μμ±ν μμ κ°μλ₯Ό μ
λ ₯ν λ€μ ν μΉΈ λκ³ μ°Ύμ λ¬Έμμ΄μ μ
λ ₯νμΈμ." << '\n';
cin >> n >> target;
cout << "μμ μ μ μμ κ°μλ§νΌ λ¬Έμμ΄μ μ
λ ₯νμΈμ. ꡬλΆμ λμ΄μ°κΈ° ν μΉΈμΌλ‘ ν©λλ€." << '\n';
for (int i = 0; i < n; i++) {
string x;
cin >> x;
arr.push_back(x);
}
// μμ°¨ νμ μν κ²°κ³Ό μΆλ ₯
cout << sequantialSearch(n, target, arr) << '\n';
}