forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1.java
More file actions
37 lines (30 loc) Β· 1.31 KB
/
1.java
File metadata and controls
37 lines (30 loc) Β· 1.31 KB
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
import java.util.*;
public class Main {
// μμ°¨ νμ μμ€μ½λ ꡬν
public static int sequantialSearch(int n, String target, String[] arr) {
// κ° μμλ₯Ό νλμ© νμΈνλ©°
for (int i = 0; i < n; i++) {
System.out.println(arr[i]);
// νμ¬μ μμκ° μ°Ύκ³ μ νλ μμμ λμΌν κ²½μ°
if (arr[i].equals(target)) {
return i + 1; // νμ¬μ μμΉ λ°ν (μΈλ±μ€λ 0λΆν° μμνλ―λ‘ 1 λνκΈ°)
}
}
return -1; // μμλ₯Ό μ°Ύμ§ λͺ»ν κ²½μ° -1 λ°ν
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("μμ±ν μμ κ°μλ₯Ό μ
λ ₯ν λ€μ ν μΉΈ λκ³ μ°Ύμ λ¬Έμμ΄μ μ
λ ₯νμΈμ.");
// μμμ κ°μ
int n = sc.nextInt();
// μ°Ύκ³ μ νλ λ¬Έμμ΄
String target = sc.next();
System.out.println("μμ μ μ μμ κ°μλ§νΌ λ¬Έμμ΄μ μ
λ ₯νμΈμ. ꡬλΆμ λμ΄μ°κΈ° ν μΉΈμΌλ‘ ν©λλ€.");
String[] arr = new String[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.next();
}
// μμ°¨ νμ μν κ²°κ³Ό μΆλ ₯
System.out.println(sequantialSearch(n, target, arr));
}
}