File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed
weekly/week01/BOJ_1920_수 찾기 Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change 1+ package stduy .week01 ;
2+
3+ import java .io .BufferedReader ;
4+ import java .io .IOException ;
5+ import java .io .InputStreamReader ;
6+ import java .util .Arrays ;
7+
8+ public class BOJ_1920 {
9+
10+ static int [] A ;
11+
12+ public static void main (String [] args ) throws IOException {
13+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
14+ StringBuilder sb = new StringBuilder ();
15+
16+ int N = Integer .parseInt (br .readLine ());
17+ String [] s = br .readLine ().split (" " );
18+ A = new int [N ];
19+ for (int i =0 ;i <N ;i ++){
20+ A [i ] = Integer .parseInt (s [i ]);
21+ }
22+ Arrays .sort (A );
23+
24+ int M = Integer .parseInt (br .readLine ());
25+ s = br .readLine ().split (" " );
26+
27+ for (int i =0 ;i <M ;i ++){
28+ int num = Integer .parseInt (s [i ]);
29+ int result = findNumByBinarySearch (0 , N - 1 , num );
30+ sb .append (result ).append ("\n " );
31+ }
32+
33+ System .out .println (sb );
34+
35+ }
36+
37+ private static int findNumByBinarySearch (int start , int end , int target ) {
38+ if (start > end ){
39+ return 0 ;
40+ }
41+
42+ int mid = (start + end ) / 2 ;
43+
44+ if (A [mid ] == target ){
45+ return 1 ;
46+ }else if (A [mid ] > target ){
47+ return findNumByBinarySearch (start , mid - 1 , target );
48+ }else {
49+ return findNumByBinarySearch (mid + 1 , end , target );
50+ }
51+ }
52+
53+ }
You can’t perform that action at this time.
0 commit comments