728x90
반응형
https://school.programmers.co.kr/learn/courses/30/lessons/250135
시간을 초로 바꿔서 각도 계산으로 겹침을 판단한다는 방향은 맞췄지만
하지만 360도가 되면 0으로 바꿔버리게 되어 겹침을 판단 못하는 문제를 발견하지 못해 시간이 좀 걸렸다.
def solution(h1, m1, s1, h2, m2, s2):
answer = 0
# 초로 모두 환산
start_time = h1 * 60 * 60 + m1 * 60 + s1
end_time = h2 * 60 * 60 + m2 * 60 + s2
# 시작 지점에 겹쳐있는 경우 카운트
if start_time == 0 or start_time == 12 * 3600:
answer += 1
while start_time < end_time:
# 시침 12시간에 360도 -> 1시간에 30도 -> 1초에 30/3600도 (1/120도)
# 분침 60분에 360도 -> 1초에 360/3600도 (0.1도)
# 초침 60초에 360도 -> 1초에 6도
h = start_time / 120 % 360
m = start_time / 10 % 360
s = start_time * 6 % 360
# 360도가 되어 0으로 돌아가 알람 체크가 안되는 것을 방지
n_h = 360 if (start_time + 1) / 120 % 360 == 0 else (start_time + 1) / 120 % 360
n_m = 360 if (start_time + 1) / 10 % 360 == 0 else (start_time + 1) / 10 % 360
n_s = 360 if (start_time + 1) * 6 % 360 == 0 else (start_time + 1) * 6 % 360
# 알람 체크
if s < h and n_h <= n_s:
answer += 1
if s < m and n_m <= n_s:
answer += 1
# 중복하여 겹쳐 있는 경우 한번만 알람이 울림
if n_s == n_m and n_s == n_h:
answer -= 1
start_time += 1
return answer
728x90
반응형
'Study > algorithm' 카테고리의 다른 글
[programmers] 양과 늑대 (python) (0) | 2024.07.04 |
---|---|
[programmers] 요격 시스템 (python) (0) | 2024.07.03 |
[programmers] PCCP 기출문제 2번 / 석유 시추 (python) (1) | 2024.07.03 |
[softeer] [인증평가(4차) 기출] 슈퍼컴퓨터 클러스터 (python) (1) | 2022.09.29 |
[softeer] [21년 재직자 대회 본선] 트럭 (python) (0) | 2022.09.14 |