-
Notifications
You must be signed in to change notification settings - Fork 0
/
EulerNineteen.java
54 lines (54 loc) · 1.09 KB
/
EulerNineteen.java
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
public class EulerNineteen
{
enum Month { Jan, Feb, Mar, Apr, June, Jul, Aug, Sept, Oct, Nov, Dec };
public static boolean isLeap(int year)
{
if (year % 400 == 0)
return true;
else if (year % 100 == 0)
return false;
else if (year % 4 == 0)
return true;
// Just to satisfy program...
else
return false;
}
public static void main(String[] args)
{
boolean leapYear = false;
int year = 1901;
int numSundays = 0;
int dayOfWeek = 2;
while (year <= 2000)
{
leapYear = isLeap(year);
for (int month=1; month <= 12; month++)
{
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
dayOfWeek+=31;
dayOfWeek %= 7;
}
if (month == 2 && leapYear)
{
dayOfWeek +=29;
dayOfWeek %= 7;
}
if (month == 2 && !leapYear)
{
dayOfWeek+=28;
dayOfWeek %= 7;
}
if (month == 4 || month == 6 || month == 9 || month == 11)
{
dayOfWeek += 30;
dayOfWeek %= 7;
}
if (dayOfWeek == 0)
numSundays++;
}
year++;
}
System.out.println(numSundays);
}
}