-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution19.java
More file actions
68 lines (67 loc) · 1.62 KB
/
Copy pathSolution19.java
File metadata and controls
68 lines (67 loc) · 1.62 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public class Solution19{
static int numDays(int month, int year){
switch(month){
case 4:
return 30;
case 6:
return 30;
case 9:
return 30;
case 11:
return 30;
case 2:
//This case checks for leap year.
//Year divisible by 4.
if(year % 4 == 0){
// if year falls on a new century.
if(year % 100 == 0){
//if century is divisible by 400.
if(year % 400 == 0){
return 29;
}
}
else{
return 29;
}
}
else{
return 28;
}
default:
return 31;
}
}
public static void main(String[] args) {
/*Days of week:
0, Sun
1, Mon
2, Tues
3, Wed
4, Thurs
5, Fri
6, Sat.
*/
//Jan 1, 1901 was a Tuesday.
int firstDay = 2;
int numSundays = 0;
for(int year = 1901; year <= 2000; year++){
int days = numDays(1, year);
int offset = days % 7;
if(year != 1901){
firstDay = (offset + firstDay) % 7;
}
if(firstDay == 0){
numSundays++;
}
for(int rest = 2; rest <= 12; rest++){
firstDay = (offset+firstDay)%7;
if(firstDay == 0){
numSundays++;
}
days = numDays(rest,year);
offset = days % 7;
}
}
System.out.println(numSundays);
}
}