๐Ÿ“š Stack vs. Deque

temprmnยท2023๋…„ 8์›” 1์ผ
0
post-thumbnail

Stack vs. Deque

Deque

LIFO???

import java.io.*;
import java.util.*;
    
public class Main {
    
    public static void main(String[] args) throws IOException {
        System.setIn(new FileInputStream("./input.txt"));
    
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int N = Integer.parseInt(br.readLine());
    
        StringTokenizer st = new StringTokenizer(br.readLine());
    
        int[] ax = new int[N];
        for(int n = 0; n < N; n++) 
            ax[n] = Integer.parseInt(st.nextToken());                  
    
        LinkedList<Integer> out = new LinkedList<Integer>();
        for(int n = N-1; n >= 0; n--) {
            int cur = N - (n);   // ํ˜„์žฌ ๊ฐ’ 
            if(ax[n] == 1) {
               out.addFirst(cur);
            } else if(ax[n] == 2) {
               if(out.size() < 2)  {
                   out.add(cur);
               } else {
                   out.add(1, cur);
               }
            } else if(ax[n] == 3) {
                out.add(cur);
            }
        }
    
        for(int n = 0; n < N; n++) {
            bw.write(out.get(n) + " ");
        }
    
        br.close();
        bw.flush();
        bw.close();
   }   
    
}
import java.io.*;
import java.util.*;
    
public class Main {
    
    public static void main(String[] args) throws IOException {
        System.setIn(new FileInputStream("./input.txt"));
    
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int N = Integer.parseInt(br.readLine());
    
        StringTokenizer st = new StringTokenizer(br.readLine());
    
        int[] ax = new int[N];
        for(int n = 0; n < N; n++) 
            ax[n] = Integer.parseInt(st.nextToken());    
    
        Deque<Integer> deq = new LinkedList<>();
        for(int n = N-1; n >= 0; n--) {
            if(ax[n] == 1) {
                deq.addFirst(N - (n));
            } else if(ax[n] == 2) {
                Integer first = deq.pollFirst();
                deq.addFirst(N - (n));
                if(first != null) {
                    deq.addFirst(first);
                } else if(ax[n] == 3) {
                    deq.add(N - (n));
                }
            }
    
        for(int n = 0; n < N; n++) {
            bw.write(deq.poll() + " ");
        }
    
        br.close();
        bw.flush();
        bw.close();
}   
    
}

BufferedReader, BufferedWriter

  • BufferedReader, BufferedWriter ํ™œ์šฉ ์ฝ”๋“œ : ๊ธฐ๋ณธ ๋ฌธ์ œ
    import java.io.BufferedReader; // ์ž„ํฌํŠธ ํ•„์ˆ˜
    import java.io.BufferedWriter;
    
    public static void main(String[] args) throws IOException {
    
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    
    	String s = br.readLine(); // bufferedwriter ์˜ ๊ธฐ๋ณธํ˜•์€ String
    		
    	int i = Integer.parseInt(s) +10; // String ์„ int๋กœ ํ˜•๋ณ€ํ™˜ ํ›„ +10 !!
    		
    	br.close(); // bufferedreader ๋„ ์ž…๋ ฅ์„ ๋งˆ์ณค๋‹ค๋ฉด ๋‹ซ์•„์ฃผ์ž
    		
    	bw.write("์ž…๋ ฅ๋ฐ›์€ ๊ฐ’ : "+ s); // ์ถœ๋ ฅ
    	bw.newLine(); // ๊ฐœํ–‰ ๋ฉ”์†Œ๋“œ
    	bw.write("์ž…๋ ฅ๋ฐ›์€ ๊ฐ’ +10 : "+i+"\n"); // ์ด๋ ‡๊ฒŒ ํ•˜๋‹ˆ๊นŒ ์ œ๋Œ€๋กœ ์ถœ๋ ฅ๋จ
    
    	bw.flush(); // ๋‚จ์€ ๊ฐ’ ์ถœ๋ ฅ && ๋ฒ„ํผ ์ดˆ๊ธฐํ™”
    	bw.close(); // bufferedwriter ๋‹ซ๊ธฐ
     }
  • BufferedReader, BufferedWriter ํ™œ์šฉ ์ฝ”๋“œ : A+B ๋ฌธ์ œ
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.util.StringTokenizer;
    
    public static void main(String[] args) throws IOException {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // ์ž…๋ ฅ
    	BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); // ์ถœ๋ ฅ
    	StringTokenizer st;
    
    	int T = Integer.parseInt(br.readLine());
    
    	for(int i = 0; i < T; i++) {
    		st = new StringTokenizer(br.readLine());
    		bw.write((Integer.parseInt(st.nextToken()) + Integer.parseInt(st.nextToken()) + "\n");
    	}
    
    	bw.close();
    	br.close();
    }

StringTokenizer

[Java] StringTokenizer ๋ฌธ์ž์—ด ๋ถ„๋ฆฌํ•˜๊ธฐ (split๊ณผ ์ฐจ์ด๋Š” ๋ญ˜๊นŒ?) (tistory.com)

profile
`ISFJ` T 49% F 51% /

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2023๋…„ 8์›” 1์ผ

์ข‹์€ ๊ธ€ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ž์ฃผ ์˜ฌ๊ฒŒ์š” :)

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ