File tree Expand file tree Collapse file tree 2 files changed +124
-0
lines changed
Expand file tree Collapse file tree 2 files changed +124
-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+
7+ // 스택
8+ public class BOJ_10828 {
9+ static class MyStack {
10+ int [] st ;
11+ int top ;
12+
13+ public MyStack (int size ){
14+ this .st = new int [size ];
15+ this .top = -1 ;
16+ }
17+
18+ public void push (int x ){
19+ st [++top ] = x ;
20+ }
21+
22+ public int pop (){
23+ if (top == -1 ){
24+ return -1 ;
25+ }
26+
27+ return st [top --];
28+ }
29+
30+ public int size (){
31+ return top + 1 ;
32+ }
33+
34+ public int empty (){
35+ if (top == -1 ){
36+ return 1 ;
37+ }
38+
39+ return 0 ;
40+ }
41+
42+ public int top (){
43+ if (top == -1 ){
44+ return -1 ;
45+ }
46+
47+ return st [top ];
48+ }
49+ }
50+
51+ public static void main (String [] args ) throws IOException {
52+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
53+ StringBuilder sb = new StringBuilder ();
54+
55+ int N = Integer .parseInt (br .readLine ());
56+
57+ MyStack stack = new MyStack (N );
58+ for (int i = 0 ; i < N ; i ++) {
59+ String []s = br .readLine ().split (" " );
60+
61+ switch (s [0 ]){
62+ case "push" : stack .push (Integer .parseInt (s [1 ])); break ;
63+ case "pop" : sb .append (stack .pop ()).append ("\n " ); break ;
64+ case "size" : sb .append (stack .size ()).append ("\n " ); break ;
65+ case "empty" : sb .append (stack .empty ()).append ("\n " ); break ;
66+ case "top" : sb .append (stack .top ()).append ("\n " ); break ;
67+ }
68+ }
69+ System .out .println (sb );
70+ }
71+ }
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