출처: https://train.usaco.org/usacogate


문제 설명

Is Friday the 13th really an unusual event?

That is, does the 13th of the month land on a Friday less often than on any other day of the week? To answer this question, write a program that will compute the frequency that the 13th of each month lands on Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday over a given period of N years. The time period to test will be from January 1, 1900 to December 31, 1900+N-1 for a given number of years, N. N is positive and will not exceed 400.

Note that the start year is NINETEEN HUNDRED, not NINETEEN NINETY.

There are few facts you need to know before you can solve this problem:

January 1, 1900 was on a Monday.
Thirty days has September, April, June, and November, all the rest have 31 except for February which has 28 except in leap years when it has 29.
Every year evenly divisible by 4 is a leap year (1992 = 4*498 so 1992 will be a leap year, but the year 1990 is not a leap year)
The rule above does not hold for century years. Century years divisible by 400 are leap years, all other are not. Thus, the century years 1700, 1800, 1900 and 2100 are not leap years, but 2000 is a leap year.
Do not use any built-in date functions in your computer language.

Don't just precompute the answers, either, please.

입력 양식

One line with the integer N.

출력 양식

Seven space separated integers on one line. These integers represent the number of times the 13th falls on Saturday, Sunday, Monday, Tuesday, ..., Friday.

입력 예

20

출력 예

36 33 34 33 35 35 34


문제 풀이 내용

매달 13일이 요일마다 몇번씩 있었는지 세는 문제이다. 1900. 1. 1 이 월요일에서 시작하기 때문에 첫번째 13일이 토요일이 되는 것을 기준으로 각 달이 며칠인지 더해서 계산했다.

프로그램 내용

더보기
#define BEGIN_YEAR 1900
#define FIRST_DAY 2 // Jan. 1. 1990 - Monday
/// Sat=0, Sun=1, Mon=2, Tue=3, Wed=4, Thu=5, Fri=6
#define DAYS_IN_WEEK 7
#define MONTH_IN_YEAR 12
#define MAX_IN_YEAR 400

/*
Leap Year : year % 4 == 0 && year % 100 != 100 && year %400 == 0
*/
bool LeapYear (int inYear)
{
    bool LeapFlag = false;

    if (inYear % 4 == 0) { LeapFlag = true; }
    if (inYear % 100 == 0) { LeapFlag = false; }
    if (inYear % 400 == 0) { LeapFlag = true; }

    return LeapFlag;
};

int Days = 13 + FIRST_DAY - 1; /// 15 Mon - Sat

    for (int index = 0; index < InYear ; ++index)
    {
        int year = BEGIN_YEAR + index;
        if(!LeapYear (year))
        {
            for (int idx =0; idx< MONTH_IN_YEAR ; ++idx)
            {
                ++Thirteen[Days%7];
                Days += DayInMonth[idx];
            }
        }
        else
        {
            for (int idx =0; idx< MONTH_IN_YEAR ; ++idx)
            {
                ++Thirteen[Days%7];
                Days += LeapDayInMonth[idx];
            }
        }
    }

    for (int index=0; index<DAYS_IN_WEEK ; ++index)
    {
        fout << Thirteen[index];
        if(index < 6)
            fout << " ";
    }

 

Chapter 1. Getting started

'USACO Training' 카테고리의 다른 글

Problem 1.3.2 Milking Cow  (0) 2019.09.07
Problem 1.2.7 Broken Necklace  (0) 2019.09.07
Problem 1.2.5 Greedy Gift Givers  (0) 2019.09.07
Problem 1.2.2 Ride  (0) 2019.09.07
USACO Training - Chapter 1. Getting started  (0) 2019.09.06

+ Recent posts