Algorithm, Data structure/Solved Algorithmic Problem

USACO 1.1 - Friday the Thirteenth

JaykayChoi 2016. 5. 29. 20:34

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 1990.

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.

PROGRAM NAME: friday

INPUT FORMAT

One line with the integer N.

SAMPLE INPUT (file friday.in)

20

OUTPUT FORMAT

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

SAMPLE OUTPUT (file friday.out)

36 33 34 33 35 35 34


출처: http://train.usaco.org/



윤년을 고려하여 N이 주어질 때 1900년부터 1900+n-1 까지의 각 달의 13일의 요일의 개수를 토요일 부터 순서대로 출력하는 문제입니다.

쉬운 문제이나 토요일부터 출력을 해야 된다는 것이 함정이네요;;




my solving 

c++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <fstream>
#include <string>
 
using namespace std;
 
int calcDaysOfMonth(int month, int year) 
{
    switch (month) 
    {
        case 2:
            if ((year % == && year % 100 != 0|| year % 400 == 0)
                return 29;
            else
                return 28;
        case 4:
        case 6:
        case 9:
        case 11:
            return 30;
        default:
            return 31;
    }
}
 
int main() 
{
    ofstream fout("friday.out");
    ifstream fin("friday.in");
 
    int N;
    fin >> N;
 
    int days[7= { 0, };
 
    int now = 0;
 
    for (int i = 1900; i < 1900 + N; i++)
    {
        for (int month = 1; month < 13; month++)
        {
            days[((now + 13) % 7)]++;
            now = (now + calcDaysOfMonth(month, i)) % 7;
        }
    }
 
    for (int i = 0; i < 7; i++)
    {
        fout << days[(i + 6) % 7];
 
        if (i == 6
            fout << endl;
        else 
            fout << " ";
    }
    return 0;
}
cs