[Java API] Class LocalTime 코딩테스트 활용예시 (백준 29735: 택배가 안와잉)

Charbs·2025년 2월 28일

algorithm

목록 보기
36/37

백준 29735번: 택배가 안와잉
위 문제를 풀다가 시간을 의미하는 문자열의 처리를 공부하기 위해 Java API Document 에서 관련한 클래스를 공부해보자. (물론 다른 방법으로도 풀 수 있다)

택배기사는 09시00분 부터 16시00분까지만 업무를 하고, 브실이의 택배가 배송되기 전까지 3개의 택배가 남았고 각 택배는 배송되는데 15분씩 걸린다.
출력으로 브실이의 택배 배송 예상 날짜, 시간을 0일 뒤에 10시00분에 도착한다고 구해보자.
단, 퇴근시간과 동시에 배송이 완료된다면 그 택배는 다음날에 배송한다. 칼퇴를 위해.


실제 대부분 기업들의 코딩테스트 환경인 프로그래머스에서는 이 문제와 같이 문자열로 날짜나 시간이 주어지는 경우가 많기에 이참에 관련 API를 숙지해보자.

Class LocalTime Document

LocalTime class 를 생성하는 static method

알고리즘, 코딩테스트 때 쓸만한 메서드만 정리해봤다.

  • static LocalTime of(int hour, int minute, [int second])
    - Obtains an instance of LocalTime from an hour and minute (second optional)
  • static LocalTime parse(CharSequence text)
    - Obtains an instance of LocalTime from a text string such as 10:15

LocalTime Instance 를 처리하는 method

비교 / 값 얻기

  • int compareTo(LocalTime other)
    • Compares this time to another time.
    • Returns : negative if less, positive if greater, 0 if equal
  • int getHour()
    • Gets the hour-of-day field
  • int getMinute()
    • Gets the minute-of-hour field
  • int getSecond()
    • Gets the second-of-minute field
  • boolean isAfter(LocalTime other)
    • Checks if this time is after the specified time
  • boolean isBefore(LocalTime other)
    - Checks if this time is before the specified time

    연산

    연산을 수행한 복사본을 return
  • LocalTime minusHours(long hoursToSubtract)
  • LocalTime minusMinutes(long minutesToSubtract)
  • LocalTime minusSeconds(long secondsToSubtract)
  • LocalTime plusHours(long hoursToAdd)
  • LocalTime plusMinutes(long minutesToAdd)
  • LocalTime plusSeconds(long secondsToAdd)

    출력

  • String toString()
    • Outputs this time as s String, such as 10:15

활용 예시코드

import java.io.*;
import java.util.*;
import java.time.*;

public class Main {
    
    static int N, T;
    static LocalTime start, end;
    static StringBuilder sb = new StringBuilder();
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        start = LocalTime.parse(st.nextToken());
        end = LocalTime.parse(st.nextToken());
        
        st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        T = Integer.parseInt(st.nextToken());
        
        int n=0;    // 보낸 택배수
        int day=0;  // 경과한 날짜
        LocalTime now = LocalTime.of(start.getHour(),start.getMinute());
        while (n<N+1) {
            if (now.plusMinutes(T).isBefore(end))   { // 해당 날에 택배 보낼 수 있음
                n++;
                now = now.plusMinutes(T);
            } else {                        // 내일 보내자
                day++;
                now = LocalTime.of(start.getHour(),start.getMinute());
            }
        }
        
        sb.append(day).append("\n").append(now.toString());
        System.out.println(sb);
        
    }
}

profile
자두과자

0개의 댓글