201003 토 [BOJ] 10866, 10430, 2609, 10799

36419 단어 boj2020.102020.10

BOJ 10866

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Deque;
import java.util.ArrayDeque;
public class Main {	

	
    public static void main(String[] args) throws IOException {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	StringBuilder sb = new StringBuilder();
    	Deque<Integer> deque = new ArrayDeque<>();
    	int N = Integer.parseInt(br.readLine());
    	
    	for(int i=0;i<N;i++) {
    		String[] arr = br.readLine().split(" ");
    		switch (arr[0]) {
    			case "push_front" : deque.offerFirst(Integer.parseInt(arr[1])); break;
    			case "push_back" : deque.offerLast(Integer.parseInt(arr[1])); break;
    			case "pop_front" : 
    				if(deque.isEmpty())
    					sb.append(-1 + "\n");
    				else
    					sb.append(deque.pollFirst() + "\n");
    				break;
    			case "pop_back" : 
    				if(deque.isEmpty())
    					sb.append(-1 + "\n");
    				else
    					sb.append(deque.pollLast() + "\n");
    				break;
    			case "size" : sb.append(deque.size() + "\n"); break;
    			case "empty" :
    				if(deque.isEmpty())
    					sb.append(1 + "\n");
    				else
    					sb.append(0 + "\n");
    				break;
    			case "front" :
    				if(deque.isEmpty())
    					sb.append(-1 + "\n");
    				else
    					sb.append(deque.peekFirst() + "\n");
    				break;
    			case "back" :
    				if(deque.isEmpty())
    					sb.append(-1 + "\n");
    				else
    					sb.append(deque.peekLast() + "\n");
    				break;    				
    		}
    	}
    	System.out.println(sb);
    }
}

Deque (Double-Ended Queue)라는 Collection을 처음 알게 되었다.

다른 사람들은 Deque을 구현할 때, ArrayDeque보다 LinkedList 구현체를 주로 쓰는 것 같다.


BOJ 10430

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {	

	
    public static void main(String[] args) throws IOException {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	StringBuilder sb = new StringBuilder();
    	
    	String[] input = br.readLine().split(" ");
    	int A = Integer.parseInt(input[0]);
    	int B = Integer.parseInt(input[1]);
    	int C = Integer.parseInt(input[2]);
    	
    	sb.append( (A+B)%C + "\n");
    	sb.append( ((A%C) + (B%C))%C + "\n");
    	sb.append( (A*B)%C + "\n");
    	sb.append( ((A%C) * (B%C))%C + "\n");
    	System.out.println(sb);
    }
}

1달 전에 풀었던데, Scanner로 풀었던 것보다 30ms나 빨라졌다.


BOJ 2609

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {	
	
    public static void main(String[] args) throws IOException {
    	//2609
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	StringBuilder sb = new StringBuilder();
    	
    	String[] input = br.readLine().split(" ");
    	int a = Integer.parseInt(input[0]);
    	int b = Integer.parseInt(input[1]);
    	
    	int gcd = (a>b)?getGcd(a,b):getGcd(b,a);	//같은 경우는?
    	int lcm = (a * b) / gcd;
    	sb.append(gcd + "\n");
    	sb.append(lcm);
    	System.out.println(sb);
    }

	private static int getGcd(int greaterOne, int lesserOne) {
		int a = greaterOne;
		int b = lesserOne;
		while(b>0) {
			int tmp = a;
			a = b;
			b = tmp % b;
		}
		return a;
	}
}

유클리드 호제법 이용


BOJ 10799

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {	
	//10799
    public static void main(String[] args) throws IOException {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	
    	String input = br.readLine();
    	
    	int newLeft = 0;
    	int onStick = 0;
    	int totalCnt = 0;
    	
    	for(int i=0;i<input.length();i++) {
    		if(input.charAt(i) == '(') {
    			if(input.charAt(i+1) == ')') {
    				totalCnt += newLeft;
    				totalCnt += onStick;
    				newLeft = 0;
    				i++;
    			} else {
	    			newLeft++;
	    			onStick++;
    			}
    		} else if(input.charAt(i) == ')') {
    			onStick--;
    		}
    	}
    	System.out.println(totalCnt);
	}
}

난 너무 naive 하게 로직을 짰고,
Stack을 이용해 푸는 방법도 있다. 그게 훨씬 직관적인듯.

좋은 웹페이지 즐겨찾기