728x90
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 54 55 56 57 58 59 60 61 62 63 64 65 | import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.Arrays; public class Main { private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); public static boolean isExist(int[] numArr, int X) { int left = 0; int right = numArr.length-1; int mid = 0; while(left<=right) { mid = (left+right)/2; if(numArr[mid]<X) left = mid+1; else if(numArr[mid]>X) right = mid-1; else return true; } return false; } public static void main(String[] args) throws IOException{ int N = stoi(br.readLine()); int[] numArr = new int[N]; String[] str = br.readLine().split(" "); for(int i=0; i<N; i++) numArr[i] = stoi(str[i]); Arrays.sort(numArr); int M = stoi(br.readLine()); str = br.readLine().split(" "); StringBuilder sb = new StringBuilder(); for(int i=0; i<M; i++) { if( isExist(numArr, stoi(str[i])) ) sb.append("1\n"); else sb.append("0\n"); } System.out.println(sb.toString()); // bw.write(""); // bw.flush(); // bw.close(); } private static int stoi(String input) { return Integer.parseInt(input); } } | cs |
'알고리즘 풀이 > 백준' 카테고리의 다른 글
★★★ [백준][Java] 1654번 랜선 자르기 (이진탐색) #2 (0) | 2021.10.15 |
---|---|
★ [백준][Java] 2805번 나무 자르기 (이진탐색) (0) | 2021.10.14 |
[백준][Java] 1935번 후위 표기식2 (스택) (0) | 2021.10.13 |
[백준][Java] 3020번 개똥벌레 (누적합) (0) | 2021.10.13 |
[백준][Java] 2346번 풍선 터뜨리기 (덱, linkedlist) (0) | 2021.10.12 |