Skip to content

Commit b0cd5c2

Browse files
committed
[Silver IV] Title: 수 찾기, Time: 652 ms, Memory: 51348 KB -BaekjoonHub
1 parent dfe0930 commit b0cd5c2

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# [Silver IV] 수 찾기 - 1920
2+
3+
[문제 링크](https://www.acmicpc.net/problem/1920)
4+
5+
### 성능 요약
6+
7+
메모리: 51348 KB, 시간: 652 ms
8+
9+
### 분류
10+
11+
이분 탐색, 자료 구조, 정렬
12+
13+
### 제출 일자
14+
15+
2024년 4월 26일 01:12:06
16+
17+
### 문제 설명
18+
19+
<p>N개의 정수 A[1], A[2], …, A[N]이 주어져 있을 때, 이 안에 X라는 정수가 존재하는지 알아내는 프로그램을 작성하시오.</p>
20+
21+
### 입력
22+
23+
<p>첫째 줄에 자연수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 줄에는 N개의 정수 A[1], A[2], …, A[N]이 주어진다. 다음 줄에는 M(1 ≤ M ≤ 100,000)이 주어진다. 다음 줄에는 M개의 수들이 주어지는데, 이 수들이 A안에 존재하는지 알아내면 된다. 모든 정수의 범위는 -2<sup>31</sup> 보다 크거나 같고 2<sup>31</sup>보다 작다.</p>
24+
25+
### 출력
26+
27+
<p>M개의 줄에 답을 출력한다. 존재하면 1을, 존재하지 않으면 0을 출력한다.</p>
28+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.io.*;
2+
import java.util.*;
3+
public class Main {
4+
public static void main(String[] args) throws IOException {
5+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
6+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
7+
int[] ll = { 0 };
8+
int[][] arys = br.lines()
9+
.filter(s -> ll[0]++ % 2 != 0)
10+
.map(line -> Arrays.stream(line.split(" ")).mapToInt(Integer::parseInt).toArray())
11+
.toArray(int[][]::new);
12+
Arrays.sort(arys[0]);
13+
for (int n : arys[1]) {
14+
bw.write(bs(arys[0], n) + "\n");
15+
}
16+
bw.flush();
17+
bw.close();
18+
br.close();
19+
}
20+
public static int bs(int[] arr, int n) {
21+
int l = 0;
22+
int r = arr.length - 1;
23+
while (l <= r) {
24+
int m = l + (r - l) / 2;
25+
if (arr[m] == n) {
26+
return 1;
27+
} else if (arr[m] < n) {
28+
l = m + 1;
29+
} else {
30+
r = m - 1;
31+
}
32+
}
33+
return 0;
34+
}
35+
}

0 commit comments

Comments
 (0)