[programmers] PCCP 기출문제 3번 / 아날로그 시계 (python)

2024. 7. 3. 16:13·Study/algorithm
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
'Study/algorithm' 카테고리의 다른 글
  • [programmers] 양과 늑대 (python)
  • [programmers] 요격 시스템 (python)
  • [programmers] PCCP 기출문제 2번 / 석유 시추 (python)
  • [softeer] [인증평가(4차) 기출] 슈퍼컴퓨터 클러스터 (python)
성장형감자
성장형감자
공부 기록
    반응형
  • 성장형감자
    단순하게
    성장형감자
  • 전체
    오늘
    어제
    • Category (65)
      • Paper review (38)
        • 2D Object detection (11)
        • 3D Object detection (20)
        • 2D Segmentation (1)
        • 2D Classification (5)
        • 3D classification (1)
      • Programming (4)
        • Python (1)
        • Linux (3)
      • Project (0)
      • Study (23)
        • algorithm (20)
        • etc. (1)
        • Radar (2)
  • 인기 글

  • 블로그 메뉴

    • 홈
  • 250x250
  • hELLO· Designed By정상우.v4.10.0
성장형감자
[programmers] PCCP 기출문제 3번 / 아날로그 시계 (python)
상단으로

티스토리툴바