코딩테스트 연습/브루트 포스

[브루트 포스] 1476 날짜 계산

멍멍코 2024. 1. 25. 22:27

https://www.acmicpc.net/problem/1476

 

1476번: 날짜 계산

준규가 사는 나라는 우리가 사용하는 연도와 다른 방식을 이용한다. 준규가 사는 나라에서는 수 3개를 이용해서 연도를 나타낸다. 각각의 수는 지구, 태양, 그리고 달을 나타낸다. 지구를 나타

www.acmicpc.net

입력

E, S, M = map(int, sys.stdin.readline().split())

세 수 E, S, M을 map을 이용하여 split()에 의해 생성된 각 문자열을 정수로 변환하여 저장한다

 

함수(날짜 계산)

def calculate_year(E, S, M):
    e, s, m = 1, 1, 1
    year = 1

    while True:
        if e == E and s == S and m == M:
            return year
        
        e, s, m = e+1, s+1, m+1
        year += 1
        
        if e > 15:
            e = 1
        
        if s > 28:
            s = 1
        
        if m > 19:
            m = 1

 

 

e, s, m = 1, 1, 1로 설정한 후 1씩 증가시키고, 각 범위를 넘어갈 경우 다시 1로 저장한다

year를 1씩 증가시키며 입력받은 E, S, M과 e, s, m이 동일할 경우 year를 반환한다.

 

전체코드

"""
지구(E): 1에서 15까지
태양(S): 1에서 28까지
달(M): 1에서 19까지
"""
import sys

def calculate_year(E, S, M):
    e, s, m = 1, 1, 1
    year = 1

    while True:
        if e == E and s == S and m == M:
            return year
        
        e, s, m = e+1, s+1, m+1
        year += 1
        
        if e > 15:
            e = 1
        
        if s > 28:
            s = 1
        
        if m > 19:
            m = 1

if __name__ == '__main__':
    E, S, M = map(int, sys.stdin.readline().split())
    print(calculate_year(E, S, M))