백준 1924번 규칙찾기/2007년

2019. 1. 17. 14:26Programming/Algorithm

반응형

문제: 2007년

1, 3, 5, 7, 8, 10, 12월은 31일까지, 2월은 28일까지, 4, 6, 9, 11월은 30일까지 있다.

각 월이 몇일까지 있는지 순서대로 배치하면 다음과 같다.

1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
31 28 31 30 31 30 31 31 30 31 30 31

예시의 입력과 출력을 살펴보자. 다음의 연관성을 찾아볼 수 있다.

1월 1일 -> 1%7 -> 1 -> MON
3월 14일 -> (1월의 모든 날짜+2월의 모든 날짜+14)%7 -> 3 -> WED
9월 2일 -> (1월의 모든 날짜+…+8월의 모든 날짜+2)%7 -> 0 -> SUN
12월 25일 -> (1월의 모든 날짜+…+11월의 모든 날짜+25)%7 -> 2 -> TUE

N월 M일이 주어졌을 때 해당 날짜의 요일은 다음과 같은 방법으로 구할 수 있다.

(N-1월까지 모든 일자를 더한 값+M)%7

0을 월요일로, 6을 일요일로 계산하고싶다면, (N-1월까지 모든 일자를 더한 값+M-1)%7을 통해서 구할 수 있다.
이를 코드로 작성하면 다음과같다.

#include <stdio.h>

int main()
{
    int input_month = 0, input_day = 0;
    int sum_days = 0;
    int array_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    char* array_days[] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
    int index=0;

    scanf("%d %d", &input_month, &input_day);

    if(input_month<2) {
        sum_days = input_day;
    } else {
        for(index=0; index<(input_month-1); index++){
            sum_days+=array_month[index];
        }

        sum_days+=input_day;
    }

    printf("%s", array_days[(sum_days-1)%7]);

    return 0;
}
반응형