The Doomsday Reappears~Sunday at the end of the century

Hey, please see the next question

Title: The Week at the End of the Century
There was a cult who claimed that December 31, 1999 was the end of the world. Of course, the rumor is self-defeating.
Some people say that December 31st at the end of a certain century in the future, if it is Monday, it will...
Interestingly, December 31st in any year at the end of the century cannot be Monday!!
So, "rumor maker" Modified to Sunday again...
December 31st in 1999 is a Friday. May I ask: Which one of the nearest centuries in the future (ie xx99) will happen to be Sunday (ie Sunday) on December 31st?

请回答该年份(只写这个4位整数,不要写12月31等多余信息)

To do this question, first of all, I think about how many leap years there are in a hundred years, and then I checked how many leap years there are from 1999 to 2099. After all, this is related to the number of days, right?
Then the second step is to know how many leap years are (usually every 100 years is 24 leap years, and every four hundred years is one leap year).
What's more, the title starts in 1999, and 2000 happens to be a leap year. Then we can set x hundred years, usually 365 days a year, the extra time is 24 days for each x, that is, the number of days is 365 * 100 * x + x * 24; but there are 25 more It's a leap year.
x/4, this is 25 leap years, if the remainder is greater than zero, then one day must be added, if it is zero, then there is no need to add one; in
short, the code is as follows:

#include <stdio.h>
int main()
{
    
    
	int start = 1999;
	int x;//x百年后会出现
	int day;//天数
	int yu;//yu = 余:是指总天数除以7之后余的天数 
	int a,b;
	for(x = 1;x<10000;x++)
	{
    
    
		day = x*100*365+x*24;
		a = x/4;//每四百年就有一次一百年中有25个闰年,平常是24个闰年 
		b = x%4;//第一个一百年里也是有25个闰年,多一天 
		if(b>0)
		{
    
    
			day = day + a + 1;
		 } 
		else{
    
    day = day + a;}
		yu = day%7;
		if(yu == 2)//余天数是两天的时候正好是周日 
		{
    
    
			break;
		}

	}
	printf("%d",start+x*100);
	
	 
}

The code runs as follows:
Insert picture description here

Guess you like

Origin blog.csdn.net/FG_future/article/details/112382114