Skip to content

Commit

Permalink
BOJ1920_수찾기 java solution
Browse files Browse the repository at this point in the history
  • Loading branch information
mong3125 committed Mar 20, 2024
1 parent b45efe9 commit 7af718d
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions mong3125/이분탐색/BOJ1920_수찾기.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package 이진탐색;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class BOJ1920_수찾기 {

static int[] A;
static int[] targets;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
A = new int[N];

StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < A.length; i++) {
A[i] = Integer.parseInt(st.nextToken());
}

int M = Integer.parseInt(br.readLine());
targets = new int[M];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < targets.length; i++) {
targets[i] = Integer.parseInt(st.nextToken());
}

Arrays.sort(A);

for (int i = 0; i < targets.length; i++) {
int now = targets[i];
System.out.println(binary(0, A.length - 1, now) ? 1 : 0);
}
}

public static boolean binary(int from, int to, int n) {
if (from > to) return false;
int mid = (from + to) / 2;

if (A[mid] == n) return true;
else if (A[mid] > n) return binary(from, mid - 1, n);
else return binary(mid + 1, to, n);
}
}

0 comments on commit 7af718d

Please sign in to comment.