cppAcademy
FROM LEARNING TO COMPETING

Intermediate.
Put your skills to the test.

Bulgarian Informatics Group D · 2007–2026

270 independently explained problems: 54 Municipal, 54 Regional, 105 National Olympiad and 57 Autumn Tournament tasks. Start with the easier tasks and work upwards. Difficulty labels are learning estimates, not official ratings; Group D also includes challenging national-level problems. The six 2017 National tasks shared by Groups C and D use their existing permanent URLs. Years follow Arena’s competition-season labels. Source clarifications and special grader requirements are noted on affected tasks.
GROUP D2015 · EasyCalendar arithmetic

Weekdays in the Year 2015

National Olympiad — Municipal round (2015 season)

Submission name: mobile.cpp

Problem summary

For each supplied 2015 date, print that date and its English weekday. Dates use English month names.

Input

n, followed by n lines containing a month name and day.

Output

First print 2015. Then print each month, day and English weekday.

Constraints

1 ≤ n < 10; all dates are valid in the non-leap year 2015.

Worked example

Input and output for the worked example discussed below.

Input

3
January 4
January 7
July 13

Output

2015
January 4 Sunday
January 7 Wednesday
July 13 Monday

Solution analysis

  1. January 1, 2015 was Thursday. Compute a date’s zero-based offset from that day by adding the lengths of all previous months and then day−1.
  2. The weekday advances once per day, so use (ThursdayIndex+offset)%7. With Monday indexed 0, Thursday has index 3. Keep fixed month-name and month-length tables; February has 28 days in 2015.
  3. January 4 has offset 3 and is Sunday. January 7 has offset 6 and is Wednesday. July 13 has offset 193 and is Monday. Output the initial year line and preserve the supplied date order.

Why it works

Summing preceding month lengths and day−1 counts exactly the number of days elapsed since January 1. Weekdays repeat every seven days, so adding that offset modulo seven produces the correct weekday for every valid date.

Complexity: O(n) time and O(1) auxiliary space because month lookup examines at most twelve fixed entries per query.

Watch out. Do not treat 2015 as a leap year. The day offset is day−1, not day. Month and weekday capitalization and the initial 2015 output line are required.

Commented C++17 solution

An independently written complete program with standard headers.

#include<algorithm>
#include<array>
#include<cmath>
#include<iostream>
#include<limits>
#include<map>
#include<numeric>
#include<set>
#include<string>
#include<vector>
using namespace std;
int main(){
    const array<string,12>months={"January","February","March","April","May","June","July","August",
        "September","October","November","December"};
    const array<int,12>lengths={31,28,31,30,31,30,31,31,30,31,30,31};
    const array<string,7>weekdays={
        "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
    int queries;
    cin>>queries;
    cout<<"2015\n";
    for(int query=0;query<queries;query++){
        string month;
        int day;
        cin>>month>>day;
        int index=0;
        int offset=day-1;
        while(months[index]!=month){
            offset+=lengths[index];
            index++;
        }
        // January 1 is Thursday, index three in a Monday-first table.
        cout<<month<<' '<<day<<' '<<weekdays[(3+offset)%7]<<'\n';
    }
}

Source: Arena Olimpiici ↗