What day is January 1st?
Background
- If you want to show the calendar in this month, after logging into
STU.ipv6.club.tw, you can type the command "cal" and press Enter. This
will show you the calendar of this month as follows.
February 2014
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
- If you want to show the calendar of a specific month, for example,
January in this year, you may issue the command "cal 1 2014".
January 2014
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
- You may wonder how the computer has the knowledge to do this task.
Actually, it is a simple calculation.
- We know that January has 31 days. As long as you can determine the
day of January 1st, you know how to print out the other days in
the calendar of January. The purpose of this exercise is to get you
acquainted with the mathematical calculation for this purpose.
- The magic formula is as follows. Suppose you want to find out the
day of January 1st in the year of YEAR. Let X = YEAR -1 (e.g., X = 2013
for the year of 2014). Then calculate D = ( (X / 400) - (X / 100) + (X
/ 4) + X + 1) % 7.
- The operator "/" denotes the integer division.
- The operator "%" denotes the integer remainder.
- If D is 0, January 1st is Sunday.
If D is 1, January 1st is Monday.
If D is 6, January 1st is Saturday.
The program may run as follows:
哪一年? 2016
第一天是星期5
哪一年? 2017
第一天是星期0
哪一年? 2011
第一天是星期6