Algorithm to improve date calculation

Source: Internet
Author: User
Tags case statement

Title Description

Known November 11, 2011 is Friday, ask yyyy mm month DD Day is the days of the week? Note Consider the case of a leap year. In particular, every hundred years do not leap, the 400-year leap in the situation.

Enter a description

Enter only one row
YYYY MM DD

Output description

Output has only one row
W

Input sample 

1599 1 1

Output sample

5

Analysis

Kimlarsson Calculation formula:
W= (d+2*m+3* (m+1)/5+y+y/4-y/100+y/400+1)%7
(need to use Switch...case statement to determine the day of the week)
Improved:
W= (d+2*m+3* (m+1)/5+y+y/4-y/100+y/400)%7+1
(Calculated results are directly corresponding to the day of the week)
Attention:
When month=1 or 2 o'clock, it needs to be changed to month+12 and year-1

Test code

1#include <stdio.h>2 3 intMainvoid)4 {5     intYear , month, day;6     intx;7     8scanf"%d%d%d", &year, &month, &Day );9     if(Month = =1|| month = =2)Ten     {   OneMonth + = A;  Ayear--;  -     }   -x = (Day +2* Month +3* (Month +1) /5+ year + year/4-year/ -+ year/ -) %7+1;  theprintf"%d\n", x); -     return 0; -}

Algorithm to improve date calculation

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.