Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

11-mong3125 #37

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions mong3125/이뢄탐색/BOJ1920_수찾기.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

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);
}
}