[Daily Blue Bridge] 11, 13 years of provincial competition Java group real question "week at the end of the century"

Hello, I am the little gray ape, a programmer who can write bugs!

Welcome everyone to pay attention to my column " Daily Blue Bridge ". The main function of this column is to share with you the real questions of the Blue Bridge Cup provincial competitions and finals in recent years, analyze the algorithm ideas, data structures and other content that exist in it, and help you learn To more knowledge and technology!

Title: Week at the end of the century

A cult once claimed that December 31, 1999 was the end of the world. Of course, the rumor was self-defeating.

Some people say that at the end of a certain century, December 31st, if it is Monday, it will...

Interestingly, December 31st of any year at the end of the century cannot be a Monday! !

So the "rumor maker" was changed to Sunday,

December 31st in 1999 is a Friday. May I ask, which one of the nearest century-end years (that is, xx99 years) from us in the future, December 31st will happen to be Sunday (that is, Sunday)?

Please answer the year, (as long as this 4-digit integer, do not write redundant information such as December 31)

Problem-solving ideas:

There are two ways to calculate this question:

The first is the conventional idea: we can calculate how many days there are in each century (pay attention to judging leap years), and then calculate the day of the week on the last day of each century based on the information that December 31, 1999 is Friday. , Know that the last day of the launch is Sunday,

The second method is to use the date API (calender) in Java to calculate the day of the week on the last day of each century.

For detailed answers about these two methods, please see the source code below

Answer source code:

The first type (conventional thinking)

package 一三年省赛真题;

import java.util.Calendar;

public class Year2013_Bt1 {

	public static void main(String[] args) {
		// 闰年366天  平年365天
//		int year = 2000;
		int centuryDay=0;	//一个世纪的天数
		int week=5;			//世纪末的最后一天是星期几
		for (int year=2000; ; year++) {
			
			if (year%100==0) {
				centuryDay = 0;	//如果是本世纪第一年 天数清零
			}
			centuryDay += isRunNian(year);
			
			//如果是本世纪最后一年
			if ((year+1)%100==0) {
				int y = centuryDay%7;
				week += y;	//算出最后一天是星期几
				if (week%7==0) {
					System.out.println(year);
					break;
				}
			}
			
		}
	}
	
	// 判断是不是闰年
	public static int isRunNian(int year) {
		if ((year%4==0&&year%100!=0)||(year%400==0)) {
			return 366;		//是闰年返回366天
		}else {
			return 365;		//不是闰年返回365天
		}
	}


}

The second (with the help of calender)

package 一三年省赛真题;

import java.util.Calendar;

public class Year2013_Bt1 {

    public static void main(String[] args) {
	Calendar calendar = Calendar.getInstance();	//建立一个日历	
	for (int year = 1999; ; year+=100) {
	    calendar.set(Calendar.YEAR, year);		//设置一个年份
	    calendar.set(Calendar.MARCH, 11);		//设置月份
	    calendar.set(Calendar.DAY_OF_MONTH, 31);	//根据月份设置天
		
	    /** 
	     * 判断这一天是周日,周日是第一天,所以等于1	
	    */
	    if (calendar.get(Calendar.DAY_OF_WEEK)==1) {	
	    	System.out.println(year);
		break;
	    }
	}
}
	
}

 

Sample output:

There are deficiencies or improvements, and I hope my friends will leave a message and learn together!

Interested friends can follow the column!

Little Grey Ape will accompany you to make progress together!

Finally, I am participating in the selection of the 2020 Blog Star, please help me to vote for it!

Vote link: https://bss.csdn.net/m/topic/blog_star2020/detail?username=weixin_44985880

Guess you like

Origin blog.csdn.net/weixin_44985880/article/details/112956991