Skip to content

Commit 0b2b5d9

Browse files
committed
[Silver IV] Title: 수 찾기, Time: 688 ms, Memory: 47124 KB -BaekjoonHub
1 parent f027931 commit 0b2b5d9

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

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+
메모리: 47124 KB, 시간: 688 ms
8+
9+
### 분류
10+
11+
자료 구조, 정렬, 이분 탐색, 집합과 맵, 해시를 사용한 집합과 맵
12+
13+
### 제출 일자
14+
15+
2025년 7월 29일 21:45:53
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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
static int[] num, qList;
6+
static int N, M;
7+
static StringBuilder sb;
8+
static StringTokenizer st;
9+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
11+
public static void main(String[] args) throws Exception{
12+
inputSetting();
13+
for(int i = 0; i < M; i++) sb.append(binarySearch(qList[i])).append("\n");
14+
15+
bw.append(sb);
16+
bw.close();
17+
}
18+
19+
static int binarySearch(int find){
20+
int l, r, m;
21+
22+
l = 0;
23+
r = N - 1;
24+
25+
while(l <= r){
26+
m = (l + r) / 2;
27+
28+
if(num[m] == find) return 1;
29+
if(num[m] < find){
30+
l = m + 1;
31+
}else if(find < num[m]){
32+
r = m - 1;
33+
}
34+
}
35+
return 0;
36+
}
37+
38+
static void inputSetting() throws Exception{
39+
sb = new StringBuilder();
40+
N = Integer.parseInt(br.readLine());
41+
num = new int[N];
42+
43+
st = new StringTokenizer (br.readLine());
44+
for(int i = 0; i < N; i++) num[i] = Integer.parseInt(st.nextToken());
45+
46+
M = Integer.parseInt(br.readLine());
47+
qList = new int[M];
48+
49+
st = new StringTokenizer(br.readLine());
50+
for(int i = 0; i < M; i++) qList[i] = Integer.parseInt(st.nextToken());
51+
52+
Arrays.sort(num);
53+
}
54+
}

0 commit comments

Comments
 (0)