# Project Euler＃19，仅当从1900年开始时，答案才正确吗？

go评论61阅读模式

Project Euler #19, Answer is only correct when starting from year 1900?

# 问题

``````public class Euler19 {
public static void main(String[] args) {
int day = 2; // 1901年1月1日是星期二，如果设置为3表示星期一，但只有在设置为2表示星期一时才能得到正确答案
int sundays = 0;
int month = 1;
int monthDays = 1;
int year = 1901;
while (year <= 2000) {
switch (month) { // 每个月的情况
case 1: // 一月
if (monthDays == 31) {
month++;
monthDays = 1;
}
break;
case 2: // 二月
if ((year % 4 == 0) && (year != 1900)) // 闰年的情况
{
if (monthDays == 29) {
month++;
monthDays = 1;
}
} else {
if (monthDays == 28) {
month++;
monthDays = 1;
}
}
break;
case 3: // 三月
if (monthDays == 31) {
month++;
monthDays = 1;
}
break;
case 4: // 四月
if (monthDays == 30) {
month++;
monthDays = 1;
}
break;
case 5: // 五月
if (monthDays == 31) {
month++;
monthDays = 1;
}
break;
case 6: // 六月
if (monthDays == 30) {
month++;
monthDays = 1;
}
break;
case 7: // 七月
if (monthDays == 31) {
month++;
monthDays = 1;
}
break;
case 8: // 八月
if (monthDays == 31) {
month++;
monthDays = 1;
}
break;
case 9: // 九月
if (monthDays == 30) {
month++;
monthDays = 1;
}
break;
case 10: // 十月
if (monthDays == 31) {
month++;
monthDays = 1;
}
break;
case 11: // 十一月
if (monthDays == 30) {
month++;
monthDays = 1;
}
break;
case 12: // 十二月
if (monthDays == 31) {
month = 1;
monthDays = 1;
year++;
}
break;
}

if (day == 1 && monthDays == 1) // 星期天是一周的第一天
{
sundays++;
}

if (day == 7) {
day = 0;
}
day++;
monthDays++;
}
System.out.println("Total Sundays: " + sundays);
}
}
``````

I've been trying to understand why the following code only gives the correct answer when the start year is set to 1900. The problem specifies that Sundays should be counted from Jan 1st, 1901.

I thought that I figured it out when realizing there is no year 0, so 1900 is actually 1901... but that didn't work when adjusting the while loop from 2000 years to 1999 years.

I am completely flabbergasted at why this gives the right answer using the wrong starting year.

``````public class Euler19
{
public static void main(String[] args)
{
int day = 2; //Jan 1st, 1901 is a Tuesday, This should be 3 but only works if set to 2 for Monday
int sundays = 0;
int month = 1;
int monthDays = 1;
int year = 1901;
while(year &lt;= 2000)
{
switch(month) //Case for each month
{
case 1:                   //January
if(monthDays == 31)
{
month++;
monthDays = 1;
}
break;
case 2:                                   //February
if((year % 4 == 0) &amp;&amp; (year != 1900)) //Leap days
{
if(monthDays == 29)
{
month++;
monthDays = 1;
}
}
else
{
if(monthDays == 28)
{
month++;
monthDays = 1;
}
}
break;
case 3:                 //March
if(monthDays == 31)
{
month++;
monthDays = 1;
}
break;
case 4:                //April
if(monthDays == 30)
{
month++;
monthDays = 1;
}
break;
case 5:                 //May
if(monthDays == 31)
{
month++;
monthDays = 1;
}
break;
case 6:                 //June
if(monthDays == 30)
{
month++;
monthDays = 1;
}
break;
case 7:                 //July
if(monthDays == 31)
{
month++;
monthDays = 1;
}
break;
case 8:                 //August
if(monthDays == 31)
{
month++;
monthDays = 1;
}
break;
case 9:                 //September
if(monthDays == 30)
{
month++;
monthDays = 1;
}
break;
case 10:                //October
if(monthDays == 31)
{
month++;
monthDays = 1;
}
break;
case 11:                //November
if(monthDays == 30)
{
month++;
monthDays = 1;
}
break;
case 12:                //December
if(monthDays == 31)
{
month = 1;
monthDays = 1;
year++;
}
break;
}
if(day == 1 &amp;&amp; monthDays == 1) //Sunday is 1st day of the week
{
sundays++;
}
if(day == 7)
{
day = 0;
}
day++;
monthDays++;
}
System.out.println(&quot;Total Sundays: &quot; + sundays);
}
}
``````

# 答案1

Your error is elsewhere: Your program is skipping the first of every month. That you happened to get the correct result when using a wrong start year is a complete coincidence.

Starting out from Monday January 1, 1900 (whether that day was really a Monday or not), you are counting correctly through January and deem January 31 a Wednesday. So far it is correct based on the prerequisite. Then comes the trouble: Since 'month' is 1 and 'monthDays' 31, you increment month to 2 and set 'monthDays' to 1 in your 'switch' statement. After your 'switch' and your two 'if' statements you increment 'day' (correct) and increment 'monthDays' to 2, which gives an incorrect value. You have now got Thursday, February 2, 1900, which disagrees with January 31 being a Wednesday.

The same incorrect increment happens every time a new month starts.

I suggest you learn to use a debugger. This will enable you to find such bugs on your own.

Your error is elsewhere: Your program is skipping the first of every month. That you happened to get the correct result when using a wrong start year is a complete coincidence.

Starting out from Monday January 1, 1900 (whether that day was really a Monday or not), you are counting correctly through January and deem January 31 a Wednesday. So far it is correct based in the prerequisite. Then comes the trouble: Since `month` is 1 and `monthDays` 31, you increment month to 2 and set `monthDays` to 1 in your `switch` statement. After your `switch` and your two `if` statements you increment `day` (correct) and increment `monthDays` to 2, which gives an incorrect value. You have now got Thursday, February 2, 1900, which disagrees with January 31 being a Wednesday.

The same incorrect increment happens every time a new month starts.

I suggest you learn to use a debugger. This will enable you to find such bugs on your own.

# 答案2

So your problem is your range, right now its from 1900 to 2000 exclusive, it should be 1901 to 2000 inclusive. In short the answer is that you need to change the while loop to `while(year&lt;=2000)`

• 本文由 发表于 2020年10月6日 10:25:27
• 转载请务必保留本文链接：https://go.coder-hub.com/64218506.html
• java
• leap-year
• switch-statement

go 49

go 62

go 67

go 76