# Algorithm: Calculate the day of the week on the first day of a year.

Source: Internet
Author: User
First, use your formula to compile a simple program:
Main ()
{Int year;/* year */

Intw;/* day of the week */

Scanf ("% d", & year );

W = (year-1) + (year-1)/4-(year-1)/100 + (year-1)/400 + 1) % 7;

Printf ("% d \ n", W );

Getch ();

}

Enter 1 during running. Expected result 1 is displayed. That is, if this formula is correct, Monday is the first day of Year 1. This is a computing premise and must be clarified first.

Then we need to know the rule of the leap year: any year that can be divided by four and cannot be divided by 100 is a leap year; any year that can be divided by 400 is also a leap year; in addition, the year is not a leap year.

Third, we need to know the 366 days of the leap year and the 365 days of the year.

After the first day of the year (hereinafter referred to as "start day"), the day of the week must be calculated as long as the difference between the day and the start day is used, when the remainder is 0, the day is the same as the first day of the day, which is also Monday; otherwise, the remainder + 1 is the day of the week.

Assume that year is input in this program, the difference between the first day of the year and the start day is:

(Year-1) * 365 + !)) ... (1)

Calculation of the number of leap years from year 1 to Year 1:

According to the leap year rule, you can know that there is a leap year every four years. In this way, the number of leap years from year 1 to Year 1 is equal to (year-1) /4, but a leap year needs to be deducted every one hundred years. The correction is as follows:

Number of leap years from year 1 to Year 1 = (year-1)/4-(Year/100)

Too many deductions! A new leap year will be added every 400, and the correction is as follows:

Number of leap years from year 1 to Year 1 = (year-1)/4-(Year/100) + (Year/400)

(1) The difference between the first day of year and the start day is:

(Year-1) * 365 + (year-1)/4-(Year/100) + (Year/400)

What is the day of the week? As mentioned above, the difference is 7 for remainder + 1:

[(Year-1) * 365 + (year-1)/4-(Year/100) + (Year/400)] % 7 + 1

1 At the end of this field can be placed in the divisor, that is:

[(Year-1) * 365 + (year-1)/4-(Year/100) + (Year/400) + 1] % 7

= [(Year-1) * (364 + 1) + (year-1)/4-(Year/100) + (Year/400) + 1] % 7

= [(Year-1) * 364 + (year-1) + (year-1)/4-(Year/100) + (Year/400) + 1] % 7

364 is a multiple of 7, and year * 364 must also be a multiple of 7, so year * 364 can be removed.

The final formula:

= [(Year-1) + (year-1)/4-(Year/100) + (Year/400) + 1] % 7

The last 1 in this [] is the 1st day of the new year. Generally, what day is the day of the week in year? You can use the following formula for calculation:

[(Year-1) + (year-1)/4-(Year/100) + (Year/400) + d] % 7

Intw;/* day of the week */

Scanf ("% d", & year );

W = (year-1) + (year-1)/4-(year-1)/100 + (year-1)/400 + 1) % 7;

Printf ("% d \ n", W );

Getch ();

}

Enter 1 during running. Expected result 1 is displayed. That is, if this formula is correct, Monday is the first day of Year 1. This is a computing premise and must be clarified first.

Then we need to know the rule of the leap year: any year that can be divided by four and cannot be divided by 100 is a leap year; any year that can be divided by 400 is also a leap year; in addition, the year is not a leap year.

Third, we need to know the 366 days of the leap year and the 365 days of the year.

After the first day of the year (hereinafter referred to as "start day"), the day of the week must be calculated as long as the difference between the day and the start day is used, when the remainder is 0, the day is the same as the first day of the day, which is also Monday; otherwise, the remainder + 1 is the day of the week.

Assume that year is input in this program, the difference between the first day of the year and the start day is:

(Year-1) * 365 + !)) ... (1)

Calculation of the number of leap years from year 1 to Year 1:

According to the leap year rule, you can know that there is a leap year every four years. In this way, the number of leap years from year 1 to Year 1 is equal to (year-1) /4, but a leap year needs to be deducted every one hundred years. The correction is as follows:

Number of leap years from year 1 to Year 1 = (year-1)/4-(Year/100)

Too many deductions! A new leap year will be added every 400, and the correction is as follows:

Number of leap years from year 1 to Year 1 = (year-1)/4-(Year/100) + (Year/400)

(1) The difference between the first day of year and the start day is:

(Year-1) * 365 + (year-1)/4-(Year/100) + (Year/400)

What is the day of the week? As mentioned above, the difference is 7 for remainder + 1:

[(Year-1) * 365 + (year-1)/4-(Year/100) + (Year/400)] % 7 + 1

1 At the end of this field can be placed in the divisor, that is:

[(Year-1) * 365 + (year-1)/4-(Year/100) + (Year/400) + 1] % 7

= [(Year-1) * (364 + 1) + (year-1)/4-(Year/100) + (Year/400) + 1] % 7

= [(Year-1) * 364 + (year-1) + (year-1)/4-(Year/100) + (Year/400) + 1] % 7

364 is a multiple of 7, and year * 364 must also be a multiple of 7, so year * 364 can be removed.

The final formula:

= [(Year-1) + (year-1)/4-(Year/100) + (Year/400) + 1] % 7

The last 1 in this [] is the 1st day of the new year. Generally, what day is the day of the week in year? You can use the following formula for calculation:

[(Year-1) + (year-1)/4-(Year/100) + (Year/400) + d] % 7

Related Keywords:

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.