“This is the 29th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

I. Problem description

There was a cult that called December 31, 1999, the end of the world. Of course, that rumor has been dispelled.

There is also a person in the future of a century of December 31, if it is a Monday meeting….

Interestingly, December 31 of any end-of-century year can never fall on a Monday!!

So, “rumor makers” was changed to Sunday……

December 31, 1999 is Friday, I would like to ask: in the future which one from the end of our recent century (i.e. Xx99) December 31 is just on Sunday (i.e. Sunday)?

Two, the title requirements

Operating limits

  • Maximum running time: 1s
  • Maximum running memory: 128M

The output

Please answer the year (only the 4-digit integer, do not include additional information such as December 31)

Third, problem analysis

December 31, 1999 is Friday. Find the next nearest end of the century. Solve the problem in two steps, first find the number of days in each year, before finding out whether the last day of the current year is Sunday.

1. Determine the year

For the year, the first judgment is not leap year, according to the leap year rule “four years a leap, 100 years do not leap, 400 years a leap”, the year meets one of the following conditions, it is a leap year.

(1) Divisible by 4 and not by 100 (e.g., 2004 was a leap year but 1900 was not)

(2) Divisible by 400 (e.g. 2000 is a leap year)

Code implementation y % = = 0 400 | | y % 4 = = 0 && y % 100! = 0Copy the code

2. Judge the date

q=(q+d%7)%7; / / the current year a few if the last day of the week (q = = 0 && n % 100 = = 99) / / meet condition {cout < < n. // Exit (0); // exit loop}Copy the code

Four, coding implementation

#include<iostream>
using namespace std;
int main(a)
{
	int i,n,d,q=5;// Initialize, d represents the day of the current year, q represents the day of the week
	for(i=2000;; i++)// Start the loop
	{
		n=i;
		if(n%400= =0||n%4= =0&&n%100! =0)// Check whether it is a leap year
		{
			d=366;
		}
		else
			d=365;
		q=(q+d%7) %7;// Determine the last day of the current year
		if(q==0&&n%100= =99)// The condition is met
		{
			cout<<n;// Output the result
			exit(0);// Exit the loop}}return 0;
}
Copy the code

5. Output results

The output is 2299

Sixth, summary and improvement

Algorithm daily practice — day 5: galaxy bombs

Day 10: Time display

Algorithm daily practice — Day 13: running exercise