자료구조 1 (참고)

sua·2022년 10월 8일
0

Baekjoon

목록 보기
154/161
post-thumbnail

1935번 후위 표기식2

문제



풀이

import java.util.Scanner;
import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Stack<Double> stack = new Stack<>();
        int n = sc.nextInt();
        sc.nextLine();
        
        double arr[] = new double[n];
        String str = sc.nextLine();
        
        for(int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        
        for(char c : str.toCharArray()) {
            if(c >= 'A' && c <= 'Z') {
                stack.push(arr[c - 'A']);
            } else if(c == '*') {
                double a = stack.pop();
                double b = stack.pop();
                stack.push(a * b);
            } else if(c == '+') {
                double a = stack.pop();
                double b = stack.pop();
                stack.push(a + b);
            } else if(c == '-') {
                double a = stack.pop();
                double b = stack.pop();
                stack.push(b - a);
            } else if(c == '/') {
                double a = stack.pop();
                double b = stack.pop();
                stack.push(b / a);
            }
        }
        double answer = stack.pop();
        System.out.print(String.format("%.2f", answer));
        
        sc.close();
    }
}



1935번 후위 표기식

문제



풀이

import java.io.*;
import java.util.Stack;

public class Main {
    static StringBuilder sb = new StringBuilder();
    
    static int priority(char c) {
        if(c == '(') {
            return 0;
        }
        if(c == '+' || c == '-') {
            return 1;
        }
        return 2;
    }

    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 str = br.readLine();
        Stack<Character> stack = new Stack<>();

        for(char c : str.toCharArray()) {
            if(c >= 'A' && c <= 'Z') {
                sb.append(c);
            } else if(c == '(') {
                stack.push(c);
            } else if(c == ')') {
                while(!stack.isEmpty()) {
                    if(stack.peek() == '(') {
                        stack.pop();
                        break;
                    }
                    sb.append(stack.pop());
                }
            } else { // 연산자일 경우
                while(!stack.isEmpty() && priority(stack.peek()) >= priority(c)) {
                    sb.append(stack.pop());
                }
                stack.push(c);
            }
        }

        while(!stack.isEmpty()) {
            sb.append(stack.pop());
        }
        bw.write(sb.toString());
        bw.close();
        br.close();
    }
}

Scanner로 하려는데 계속 틀렸다고 떠서.. BufferedReader 썼더니 정답처리됨



10808번 알파벳 개수

문제


풀이

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        int arr[] = new int[26];
        
        for(char c : str.toCharArray()) {
            arr[(int)c - 'a']++;
        }
        
        for(int i : arr) {
            System.out.print(i + " ");
        }
        
        sc.close();
    }
}



10809번 알파벳 찾기

문제



풀이

import java.util.Scanner;
 
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
 
		int[] arr = new int[26];
		
		for(int i = 0; i < arr.length; i++) {
			arr[i] = -1;
		}
 
		String str = sc.next();
 
		for(int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			if(arr[c - 'a'] == -1) {
				arr[c - 'a'] = i;
			}
		}
 
		for(int a : arr) {	
			System.out.print(a + " ");
		}
        
        sc.close();
	}
}



10820번 문자열 분석

문제



풀이

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        while(sc.hasNext()) {
            String str = sc.nextLine();
            int sm = 0; // 소문자
            int bg = 0; // 대문자
            int d = 0; // 숫자
            int b = 0; // 공백
            
            for(char c : str.toCharArray()) {
                if(c == ' ') {
                    b++;
                } else if(Character.isDigit(c)) {
                    d++;
                } else {
                    if(Character.isUpperCase(c)) {
                        bg++;
                    } else {
                        sm++;
                    }
                }
            }
            System.out.println(sm + " " + bg + " " + d + " " + b);
        }
        
        sc.close();
    }
}



2743번 단어 길이 재기

문제


풀이

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));
        String str = br.readLine();
        System.out.println(str.length());
    }
}



11655번 ROT13

문제



풀이

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();

        for(char c : str.toCharArray()) {
            if(c >= 'A' && c <= 'Z') { // A ~ Z : 65 ~ 90
                c += 13;
                if(c > 'Z') {
                    c -= 26;
                }
            } else if(c >= 'a' && c <= 'z') { // a ~ z : 97 ~ 122
                c += 13;
                if(c > 'z') {
                    c -= 26;
                }
            }
            System.out.print(c);
        }
        
        sc.close();
    }
}



10824번 네 수

문제


풀이

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        long a = sc.nextInt(); long b = sc.nextInt();
        long c = sc.nextInt(); long d = sc.nextInt();
        long answer = (long) (a * Math.pow(10, String.valueOf(b).length()) + b + (c * Math.pow(10, String.valueOf(d).length())) + d);
        System.out.println(answer);

        sc.close();
    }
}



11656번 접미사 배열

문제



풀이

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        String arr[] = new String[str.length()];

        for(int i = 0; i < arr.length; i++) {
            arr[i] = str.substring(i); // i 번째부터 문자열 가져옴
        }
        Arrays.sort(arr); // 사전순으로 정렬

        for(String s : arr) {
            System.out.println(s);
        }

        sc.close();
    }
}
profile
가보자고

0개의 댓글