Waterloo Cup ADV-127 algorithm improves date calculation (Java) __ algorithm

Source: Internet
Author: User

Algorithm increase date Calculation time limit: 1.0s memory Limit: 256.0MB problem description known November 11, 2011 is Friday, ask yyyy year MM month DD Day is the week. Take note of the leap year. In particular, every hundred years does not leap, every 400-year leap situation. Input format input only one line
YYYY MM DD output format output only one row
W Data size and convention 1599 <= YYYY <= 2999
1 <= MM <= 12
1 <= DD <= 31, and ensure that test sample yyyy year mm month DD is a reasonable date
1 <= W <= 7, respectively representing Monday to Sunday sample input 2011 11 11 Sample Output 5


pay attention to some points:

int sum = (sum2-sum1 + 12)% 7;

Actually this line of code, logically speaking +5 and +12 also can, but if + 5 o'clock, at this time 11-11=0+5%7=2, not equal to. (Think for yourself) this is wrong.


for (int i=1599; i<year; i++) {

And this line of code should also pay attention to, if the time i<=year, that even the year's full of the number of years, this is wrong oh ...



idea:

Calculate the number of days from November 11, 2011 to January 1, 1599, and then calculate the required date to January 1, 1599 of the number of days, 2 for the difference of 7 to find out how many weeks


The code is as follows:

Import Java.util.Scanner;
	public class Main {static int[] Mon = {0,31,28,31,30,31,30,31,31,30,31,30,31};
		public static void Main (string[] args) {Scanner in = new Scanner (system.in);
		int year = In.nextint ();
		int month = In.nextint ();
		int day = In.nextint ();
		int sum1 = cal (2011,11,11);
		int sum2 = cal (Year,month,day);
		System.out.println (sum1+ "" +sum2);
		System.out.println (SUM1);
			if (sum1 <= sum2) {//+5 and +12 can also be, +12 is the cause when sum2 is 11.
			int sum = (sum2-sum1 + 12)% 7;
			if (sum = = 0) sum = 7;
		SYSTEM.OUT.PRINTLN (sum);
			else {int sum = (sum1-sum2)% 7;
			if (sum = = 0) System.out.println (5);
			else if (sum = = 1) System.out.println (4);
			else if (sum = = 2) System.out.println (3);
			else if (sum = = 3) System.out.println (2);
			else if (sum = = 4) System.out.println (1);
			else if (sum = = 5) System.out.println (7);
		else if (sum = = 6) System.out.println (6); } public static int cal (int year,int month,int day) {int sum =0;
		Note Here is i<year, if it is i<=year, the year also counted! for (int i=1599; i<year; i++) {if ((i% 4 = 0 && i% 100!= 0) | | (i% 400 = 0))
			{sum = 366;
			else {sum = 365; }//Note This is i<month for (int i=1; i<month; i++) {//Is the second month of a leap year, and the days are 29 days if ((year% 4 = 0 && Year% 10 0!= 0) | | (Year% 400 = 0))
			&& i = = 2) sum + + mon[i] + 1;
		else sum + = mon[i];
		sum = day;
	return sum;
 }

}

http://blog.csdn.net/qq_25605637/article/details/54897110

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.