The date some days later
For each test you are given a date and a number of days k. Print the date k days later. Work it out yourself, without a date library.
Months have 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 and 31 days, except that February has 29 in a leap year. A year is a leap year when it divides by 4, unless it divides by 100 and not by 400: 2024 and 2000 are leap years, 1900 and 2023 are not. These rules hold for every year in the tests.
k can be up to a million days, close to 2,738 years, so moving one day at a time is too slow for the largest tests.
Input
The first line holds T, the number of tests. Each test is one line holding four whole numbers: the day, month and year of a real date, then k.
Output
For each test, one line: the day, month and year of the date k days later, separated by spaces, with no leading zeros.
- Input
5
28 2 2024 1
28 2 1900 1
31 12 1999 1
1 1 2000 366
15 8 1947 0- Output
29 2 2024
1 3 1900
1 1 2000
1 1 2001
15 8 1947
- 2024 divides by 4 and not by 100, so February has 29 days.
- 1900 divides by 100 and not by 400: not a leap year.
- The last day of a year is followed by 1 January.
- 2000 divides by 400, so it is a leap year of 366 days.
- 0 days later is the same day.
Limits
1 <= T <= 10001 <= year <= 5000, and the day and month make a real date0 <= k <= 1000000
Hints
Hint 1
Turn the date into a single number, the days since 1 January of year 1, add k, and turn the number back into a date.
Hint 2
Days before year y: 365 * (y - 1) plus one for every leap year before it, which is (y - 1) / 4 - (y - 1) / 100 + (y - 1) / 400 with whole-number division.
Where this was reported
Our own problem on the task these candidates describe; the wording, examples and tests are InternDoor’s.
Run your code to check it on the examples. Submit runs every hidden test.
