문제링크
https://www.acmicpc.net/problem/1920
package BOJ;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main_1920 {
static int N,M;
static int arr[];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
N = Integer.parseInt(br.readLine());
arr = new int[N];
st = new StringTokenizer(br.readLine());
for(int i=0;i<N;i++) arr[i] = Integer.parseInt(st.nextToken());
Arrays.sort(arr);
M = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
for(int i=0;i<M;i++){
int findNum = Integer.parseInt(st.nextToken());
//findNum 이분 탐색으로 찾아보기
int left = 0;
int right = N-1;
boolean isFind = false;
while(left<=right){
int mid = (left+right)/2;
//mid 보다 findNum이 크다면 left를 mid+1로
if(arr[mid] <findNum) left = mid +1;
//mid모다 findNum이 작으면 right를 mid-1로
else if(arr[mid]> findNum) right = mid-1;
else{
isFind = true;
break;
}
}
System.out.println(isFind?"1":"0");
}
}
}
'알고리즘 > 백준' 카테고리의 다른 글
[JAVA]백준_19942_다이어트 (0) | 2021.08.09 |
---|---|
[JAVA]백준_10816_숫자카드2 (0) | 2021.08.07 |
[JAVA]백준_9012_괄호 (0) | 2021.07.19 |
[JAVA]백준_14722_우유도시 (0) | 2021.07.18 |
[JAVA]백준_2644_촌수계산 (0) | 2021.06.26 |