英文:
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 <= 2000)
{
switch(month) //Case for each month
{
case 1: //January
if(monthDays == 31)
{
month++;
monthDays = 1;
}
break;
case 2: //February
if((year % 4 == 0) && (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 && monthDays == 1) //Sunday is 1st day of the week
{
sundays++;
}
if(day == 7)
{
day = 0;
}
day++;
monthDays++;
}
System.out.println("Total Sundays: " + sundays);
}
}
答案1
得分: 3
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
得分: 0
你的问题是你的范围,现在是从1900到2000(不包括2000),应该改为从1901到2000(包括2000)。简而言之,答案是你需要将while循环更改为while(year<=2000)
。
英文:
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<=2000)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论