GESP C++ Level 3 Programming (52C++): Calendar Creation – Difficulty Five Stars

GESP C++ Level 3 Programming (46C++): [GESP202509 Level 3] Calendar Creation

This problem has stumped many candidates. There are various approaches to solving it.

1. Calculate the calendar for each month yourself and then print it. It may be a bit clumsy, but it can earn points.

2. Calculate which day of the week the first of each month falls on, do it manually, and then loop through the input.

3. Knowing that September 1 is a Monday, if the month is greater than 9, add the date % 7. If it is less than 9, add the date % 7 and then subtract from 7.

This problem covers many knowledge points and is a very representative question.

If you also include the year input, it becomes much more difficult, as you have to account for leap years and common years, where the number of days in each month varies, affecting the weekdays.

See the question:GESP C++ Level 3 Programming (52C++): Calendar Creation - Difficulty Five StarsGESP C++ Level 3 Programming (52C++): Calendar Creation - Difficulty Five StarsThis is the dataGESP C++ Level 3 Programming (52C++): Calendar Creation - Difficulty Five Stars

Solution Approach:

1. Use the known fact that September 1 is a Monday. If the month is greater than 9, add the number of days % 7. If it is less than 9, add the number of days % 7 and then subtract from 7.

#include <bits/stdc++.h>using namespace std;int main(){    int month;    cin >> month;    int week = 0, date = 0;    int daysInMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};    if (month > 9) // Greater than 9, add forward    {        for (int i = 9; i < month; i++)        {            week += daysInMonth[i];        }        week = week % 7;    }    if (month < 9)// Less than 9, add backward    {        for (int i = 8; i > month - 1; i--)        {            week += daysInMonth[i];        }        week = week % 7;        week = 7 - week; // Need to do reverse subtraction.    }    cout << "MON TUE WED THU FRI SAT SUN" << endl;    for (int i = 1; date <= daysInMonth[month] - 1; i++)    {        date++;        while (week > 0)        {            printf("    ");// Output spaces until the first day of the month.            week--;            i++;        }        printf("%3d", date);// 3 space right alignment        if (i % 7 == 0)        {            cout << endl;            continue;        }        printf(" ");    }}

Tip 1: The array is zero-indexed, so the index does not match the month, hence we use 0 as a placeholder. This way, we don’t have to worry about subtracting 1 from the month each time.

    int daysInMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

Tip 2: If it is August, the week needs to be calculated as 7 – week, counting backward; this indicates the starting and ending weekdays of the month.

 if (month < 9)    {        for (int i = 8; i > month - 1; i--)        {            week += daysInMonth[i];        }        week = week % 7;        week = 7 - week;    }

If you really can’t come up with an algorithm during the exam, you can enumerate and print all possible dates as a fallback.GESP C++ Level 3 Programming (52C++): Calendar Creation - Difficulty Five StarsHave you learned it?GESP C++ Level 3 Programming (52C++): Calendar Creation - Difficulty Five Stars

Leave a Comment