풀이
몫과 나머지를 분, 시 개념과 접목시켜서 풀어야 한다.
일단 첫째 줄에 현재 시각이 나와야 하는데, 시 분 형태로 나와야 한다.
첫 줄에 2개의 요소가 나와야 하니까 h, m = map(int, input().split( ))
다음에 더해질 시간은 t = int(input())으로 입력한다.
처음에는 if문을 사용하려고 했는데 이렇게 하면 안될 것 같아서 방법을 바꿨다.
안되는 이유는 h가 24가 되면 0으로 표기되어야 하는데
t가 얼마일줄 모르는 상황에서 그렇게 기준을 잡을 수가 없었다.
그래서 t와 기존의 시간을 활용하는 방식으로 x, y를 두고 풀었다.
y = (m+t)%60 # %는 나머지
분은 기존의 m과 t를 더한 후 60(분)으로 나눴을 때 그 나머지 값을 보여준다.
x = (h+((m+t)//60))%24
시는 (m+t)//60 으로 여기서 오버된 시를 찾고 기존의 h와 더한 후 총합을 24로 나눴다.
예를 들어 h = 14, m = 20, t = 40 이라고 하면
14 20
40
15 0
이렇게 나오려면 40+20 했을 때 60이 나오는데
그러면 위로 한 번 올려줘야 하기 때문에
올려준 후 나머지 값인 (m+t)%60 을 구하면 된다.
그럼 14는 15로 올라가야 하는데
그러려면 한 번 올라간 값 ( (m+t)//60 ) 을 기존의 h에 더해주고
그 총합을 24로 나눠주면 된다.
24로 나누는 이유는 그래야 24가 0으로 나타나야 하기 때문이다.
h = 23, m = 40, t = 30 일 때
23 40
30
0 10
이렇게 나오려면 h가 24로 나누고 나머지여야 하므로
몫과 나머지를 활용해서 풀면 된다!
코드
h, m = map(int, input().split())
t = int(input())
x = (h+((m+t)//60)) % 24
y = (m+t)%60
print(x, y)
'🥧 Python > ⚙️ 코딩테스트' 카테고리의 다른 글
[백준-Python] 2914번: 저작권 (0) | 2023.02.15 |
---|---|
[백준-Python] 2530번: 인공지능 시계 (0) | 2023.02.15 |
[백준-Python] 7287번: 등록 (0) | 2023.02.14 |
[백준-Python] 10699번: 오늘 날짜 (0) | 2023.02.14 |
[백준-Python] 11022번: A+B -8 (0) | 2023.02.14 |