알고리즘(백준 등) 공부/백준(자바)

백준 1340번: 연도 진행 바

posite 2026. 3. 26. 15:23

https://www.acmicpc.net/problem/1340

 

오늘 날짜, 시간이 주어질 때, 이번 해가 몇%지났는지 구하는 문제이다. 문제의 힌트에 평년의 각 달의 일수가 정해져 있으며, 윤년에는 2월이 29일이다. 윤년은 4로 나누어 떨어지면서 100으로 나누어 떨어지지 않거나 400으로 나누어 떨어지는 해를 의미한다. 주어진 날짜까지 지간 기간을 속한 해의 총 시간 백분율을 구하면 된다. 

 

먼저 주어진 날짜가 속한 해의 총 시간을 분 단위로 계산한다. 이를 위해 입력으로 부터 윤년인지 확인한 후, 주어진 날짜가 속한 해의 각 달의 일수를 수정한 후 더하여 분 단위로 바꾸어 준다.

String input = br.readLine();
String[] split = input.split(" ");
int year = Integer.parseInt(split[2]);
int[] dayInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
    dayInMonth[2] = 29;
}
double totalDays = 0;
for (int i = 1; i <= 12; i++) {
    totalDays += dayInMonth[i];
}
totalDays *= (60 * 24);

 

 

이후, 현재 시간을 구한다. 주어진 달의 문자열을 숫자로 바꾸기 위해 Map을 사용하였으며 그냥 조건문으로 해도 상관 없다. 주어진 날짜의 달 직전까지 일 수를 더한 후, 주어진 일 수 -1 또한 더해준다. 분 단위로 바꾸어 준 후, 현재 시간을 분 단위로 더해주면 현재 시간을 구할 수 있다. 이후에는 백분율을 구하면 되므로 생략한다.

Map<String, Integer> map = new HashMap<>();
map.put("January", 1);
map.put("February", 2);
map.put("March", 3);
map.put("April", 4);
map.put("May", 5);
map.put("June", 6);
map.put("July", 7);
map.put("August", 8);
map.put("September", 9);
map.put("October", 10);
map.put("November", 11);
map.put("December", 12);

double today = 0;
for (int i = 1; i < map.get(split[0]); i++) {
    today += dayInMonth[i];
}
today += Integer.parseInt(split[1].substring(0, split[1].length() - 1)) - 1;
today *= (60 * 24);
int time = Integer.parseInt(split[3].substring(0, 2)) * 60 + Integer.parseInt(split[3].substring(3));
today += time;

 

 

결과 코드는 다음과 같다.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class 연도진행바1340 {
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine();
        String[] split = input.split(" ");
        int year = Integer.parseInt(split[2]);
        int[] dayInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            dayInMonth[2] = 29;
        }
        double totalDays = 0;
        for (int i = 1; i <= 12; i++) {
            totalDays += dayInMonth[i];
        }
        totalDays *= (60 * 24);
        
        Map<String, Integer> map = new HashMap<>();
        map.put("January", 1);
        map.put("February", 2);
        map.put("March", 3);
        map.put("April", 4);
        map.put("May", 5);
        map.put("June", 6);
        map.put("July", 7);
        map.put("August", 8);
        map.put("September", 9);
        map.put("October", 10);
        map.put("November", 11);
        map.put("December", 12);
        
        double today = 0;
        for (int i = 1; i < map.get(split[0]); i++) {
            today += dayInMonth[i];
        }
        today += Integer.parseInt(split[1].substring(0, split[1].length() - 1)) - 1;
        today *= (60 * 24);
        int time = Integer.parseInt(split[3].substring(0, 2)) * 60 + Integer.parseInt(split[3].substring(3));
        today += time;
        System.out.print((today * 100) / totalDays);
    }
}