Random Walking Assets

a blog of software engineer, dreamer and daddy

Friday

| Comments

Problem: here

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 ID: chinux1
 PROG: friday
 LANG: C++11
 */

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>

using namespace std;


int getFebDays(int year);
bool isLeapYear(int year);

int getDaysForMonth(int year, int month)
{
    switch (month) {
        case 2:
            return getFebDays(year);
            break;
        case 4:
        case 6:
        case 11:
        case 9:
            return 30;
            break;
        default:
            return 31;
            break;
    }

}

int getFebDays(int year)
{
    if (isLeapYear(year)) {
        return 29;
    } else {
        return 28;
    }
}

bool isLeapYear(int year)
{
    if (year % 100 == 0 && year % 400 == 0) {
        return true;
    } else if (year % 100 == 0){
        return false;
    } else if (year % 4 == 0){
        return true;
    } else {
        return false;
    }
}

static vector<int> frequency = {0, 0, 0, 0, 0, 0, 0};

int main() {
    ofstream fout ("friday.out");
    ifstream fin ("friday.in");

    // Line 1
    int N = 0;
    fin >> N;

    // Compute Date for Jan 1st 1900
    int firstDayOfTheMonth = 0;

    for (int i = 0; i < N; i++) {
        int year = 1900 + i;

        for (int j = 1; j <= 12; j++) {
            frequency[(firstDayOfTheMonth + 12) % 7]++;
            // Update to next month
            firstDayOfTheMonth = (firstDayOfTheMonth + getDaysForMonth(year, j)) % 7;

        }

    }

    for (int i = 5; i < 12; i++) { // Starting from Saturday.
        fout << frequency[i % 7];
        if (i == 11) {
            fout << endl;
        } else
            fout << " ";
    }

    return 0;
}

Comments