매일 Algorithm

신재원·2023년 3월 17일
0

Algorithm

목록 보기
68/243

백준 10773번 (구현)

import java.util.*;
public class problem205 {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int size = in.nextInt();
        Stack <Integer> stack = new Stack<>();
        int sum = 0;
        for(int i = 0; i < size; i++){
            int n = in.nextInt();

            if(n == 0){
                // 0이 입력될경우 stack의 맨 위의 값을 꺼내 빼준다.
                sum -= stack.pop();
            }else{
                stack.push(n);
                sum += n;
            }
        }
        System.out.print(sum);
    }
}

백준 1924번 (구현)

import java.util.*;
public class problem206 {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int n = in.nextInt();
        int m = in.nextInt();

        String[] days = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
        int[] month = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        int total = m; // 주어진 일 수 누적
        for(int i = 0; i < n - 1; i++){
            total += month[i];
        }
        String day = days[total%7];

        System.out.print(day);
    }
}

백준 11721번 (구현)

import java.util.*;
public class problem207 {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        String st = in.nextLine();
        int length = st.length();
        int index = 0;
        while(index < length) {
            int end = index + 10;
            // 단어의 갯수가 10개씩 자르다 10개 이하로 남은경우
            if (end > length) {
                end = length;
            }
            String result = st.substring(index, end);
            index += 10;
            System.out.println(result);
        }
    }
}

백준 2455번 (구현)

import java.util.*;
public class problem208 {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int [][] t = new int [4][4];


        int max = 0;

        int sum = 0;
        for(int i = 0; i < t.length; i++){
            t[i][0] = in.nextInt(); // 내린사람
            t[i][1] = in.nextInt(); // 탄사람
            sum += t[i][1] - t[i][0];
            max = Math.max(max, sum);
        }

        System.out.print(max);
    }
}

백준 1032번 (구현)

import java.util.Scanner;

public class problem209 {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int t = in.nextInt();

        String[] arr = new String[t];
        for (int i = 0; i < t; i++) {
            arr[i] = in.next();
        }

        // 첫번째 입력된 문자를 기준으로 비교
        boolean flag = true;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr[0].length(); i++) {
            char ch = arr[0].charAt(i);

            flag = true;
            // 입력된 테스트 (t)의 갯수만큼 반복한다.
            for (int j = 1; j < t; j++) {
                if(ch != arr[j].charAt(i)){
                    flag = false;
                    break;
                }
            }
            if(flag){
                sb.append(ch);
            }
            // flag가 false일시 = 첫번째 입력된 문자열의 문자가 다를경우
            else{
                sb.append("?");
            }
        }
        System.out.print(sb.toString());
    }
}


0개의 댓글