PS

[백준/Python] (B3) Cuckoo! Cuckoo! - 30544

MSHUN 2024. 3. 13.
반응형

Baekjoon Online Judge의 30544 Cuckoo! Cuckoo! 문제의 Python 풀이입니다.

 

30544번: Cuckoo! Cuckoo!

The cuckoo bird pops out of the cuckoo clock and sounds off once on the quarter hour, half hour, and three-quarter hour. At the beginning of each hour, it sounds off the hour (1--12). Given the current time and a target number $N$, your task is to determin

www.acmicpc.net

💻코드

# 현재 시간(hh:mm)을 입력받아 시간과 분을 정수형으로 변환
current_hour, current_minute = map(int, input().split(':'))
# 목표로 하는 뻐꾸기 소리 횟수를 정수형으로 입력받음
target_sounds = int(input())

# 시작 시간에 뻐꾸기가 울리는 경우를 처리
if current_minute == 0:
    target_sounds -= current_hour
elif current_minute in [15, 30, 45]:
    target_sounds -= 1
elif current_minute < 45:
    # 다음 뻐꾸기 울림 시간까지의 시간을 계산
    current_minute = int(current_minute / 15 + 1) * 15
    target_sounds -= 1
else:
    # 시간을 1시간 올리고 분을 0으로 조정
    current_minute = 0
    current_hour += 1
    target_sounds -= current_hour

# 목표 뻐꾸기 소리 횟수에 도달할 때까지 시간을 증가
while target_sounds > 0:
    current_minute += 15
    if current_minute == 60:
        # 분이 60에 도달하면 시간을 1시간 올리고 분을 0으로 조정
        current_minute = 0
        current_hour += 1
        # 12시간 주기로 시간 조정
        if current_hour > 12:
            current_hour -= 12
        target_sounds -= current_hour
    else:
        target_sounds -= 1

# 최종 시간을 형식에 맞춰 출력
print(f"{current_hour:02d}:{current_minute:02d}")

🧠풀이

이 문제는 주어진 시간에서 뻐꾸기가 목표로 하는 횟수만큼 소리를 낼 때까지의 시간을 계산하는 문제다. 뻐꾸기 시계는 특정 규칙을 따라 소리를 내는데, 이를 코드로 구현하면서 주어진 조건을 만족시켜야 한다.


시작 시간 처리: 먼저 현재 시간을 기준으로 뻐꾸기가 소리를 내야 하는 상황인지를 판단한다. 시간이 정각이거나 15분, 30분, 45분일 때는 뻐꾸기가 소리를 내므로, 이를 고려하여 목표 횟수에서 빼준다.


목표 횟수 도달까지의 시간 증가: 현재 시간부터 시작해서 15분 간격으로 시간을 증가시키며, 각 시간마다 뻐꾸기 소리를 내는 횟수를 목표 횟수에서 차감한다. 정시에 도달할 때마다 해당 시간만큼 횟수를 차감한다.

🤔느낀 점

정각에는 시간만큼 뻐꾸기가 울린다는것을 못봐서 틀렸었다...

항상 문제를 잘 읽도록 하자...

Baekjoon Online Judge

반응형

댓글