문제

숫자 카드는 정수 하나가 적혀져 있는 카드이다. 상근이는 숫자 카드 N개를 가지고 있다. 정수 M개가 주어졌을 때, 이 수가 적혀있는 숫자 카드를 상근이가 가지고 있는지 아닌지를 구하는 프로그램을 작성하시오.

입력

첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,000,000보다 작거나 같다. 두 숫자 카드에 같은 수가 적혀있는 경우는 없다.

셋째 줄에는 M(1 ≤ M ≤ 500,000)이 주어진다. 넷째 줄에는 상근이가 가지고 있는 숫자 카드인지 아닌지를 구해야 할 M개의 정수가 주어지며, 이 수는 공백으로 구분되어져 있다. 이 수도 -10,000,000보다 크거나 같고, 10,000,000보다 작거나 같다

출력

첫째 줄에 입력으로 주어진 M개의 수에 대해서, 각 수가 적힌 숫자 카드를 상근이가 가지고 있으면 1을, 아니면 0을 공백으로 구분해 출력한다.

예제 입력 1
5
6 3 2 10 -10
8
10 9 -5 2 3 4 5 -10

예제 출력 1
1 0 0 1 1 0 0 1

내가 푼 코드

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
44
45
46
47
48
49
50
51
52
53
import java.util.*;
import java.io.*;
public class Main {
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {

int n = Integer.parseInt(br.readLine());
int[] card = new int[n];
StringTokenizer st = new StringTokenizer(br.readLine());
//카드 뽑기
for(int i=0; i<n; i++) {
card[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(card);
int m = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
//카드 뽑기
for(int i=0; i<m; i++) {
int num = Integer.parseInt(st.nextToken());
if(isContain(num, card)) {
bw.write("1 ");
} else {
bw.write("0 ");
}
}
bw.close();
br.close();

}
public static boolean isContain(int num, int[] card) {
int left = 0;
int right = card.length - 1;
boolean iscontain = false;
while (left <= right) {
int midIdx = (left + right)/2;
int midVal = card[midIdx];
if(num < midVal) {
right = midIdx-1;
} else if(num > midVal) {
left = midIdx+1;
} else {
iscontain = true;
break;
}
}
if(iscontain) {
return true;
} else {
return false;
}
}
}

풀이

처음에 문제를 풀때 이진 탐색을 이용해서 풀었는데, 시간 초과가 떴다. 따로 ide에서 돌려봤을 때는 출력도 맞게 나왔었다. 다른 정답들과 비교를 해보니까 Scanner를 사용하지 않고 BufferedReader를 사용해서 입력을 받았다. 내 코드에서 Scanner로 받은 부분을 BufferedReader를 통해 입력 받아주었더니 통과할 수 있었다.

Scanner를 사용하여 푼 코드

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
44
45
46
47
48
49
50
51
52
import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//Queue<Integer> q = new LinkedList<>();
//Stack<Integer> st = new Stack<>();
int n = sc.nextInt();
int[] card = new int[n];

//카드 뽑기
for(int i=0; i<n; i++) {
int num = sc.nextInt();
card[i] = num;
}
int m = sc.nextInt();
//카드 뽑기
for(int i=0; i<m; i++) {
int num = sc.nextInt();
if(isContain(num, card)) {
System.out.print(1);
} else {
System.out.print(0);
}
}

}
public static boolean isContain(int num, int[] card) {
Arrays.sort(card);
int left = 0;
int right = card.length - 1;
boolean iscontain = false;
while (left <= right) {
int midIdx = (left + right)/2;
int midVal = card[midIdx];
if(num < midVal) {
right = midIdx-1;
} else if(num > midVal) {
left = midIdx+1;
} else {
iscontain = true;
break;
}
}
if(iscontain) {
return true;
} else {
return false;
}
}

}