-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from AlgoLeadMe/11-mong3125
11-mong3125
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |