728x90
5074번: When Do We Finish?
For each line of input, produce one line of output containing a single time, also in the format hh:mm, being the end time of the event as a legal 24 hour time. If this time is not on the same day as the start time, the time will be in the format hh:mm +n,
www.acmicpc.net
문제 풀이
이 문제는 시작 시간과 소요 시간이 주어지고, 끝나는 시간을 출력하는 문제이다. 먼저, 형식에 맞게 입력값을 받아준 뒤, 시작 시간에 소요 시간을 더해주고, 결과가 24시간 이상이라면, 일 수를 따로 빼서 출력해주어야 한다.
주의할 점은 10 이하의 수를 출력할 경우 숫자 앞에 0을 붙여주어 자릿수를 맞춰주어야한다.
파이썬에서는 zfill이라는 함수를 통해 쉽게 숫자 앞에 0을 추가할 수 있다. 단, zfill이 문자열 형식에 대한 함수이므로 문자열로 변환한 뒤 사용하자.
정답 코드
while True :
a, b = input().split()
ah, am = map(int, a.split(":"))
bh, bm = map(int, b.split(":"))
if ah == am == bh == bm == 0 : break
ft, fm = ah + bh, am + bm
if fm > 59 :
ft += 1
fm -= 60
if ft > 23 : print("{0}:{1} +{2}".format(str(ft % 24).zfill(2), str(fm).zfill(2), ft // 24))
else : print("{0}:{1}".format(str(ft).zfill(2), str(fm).zfill(2)))
728x90