我想问问某人他/她是否可以帮助我找到我在这个问题中丢失 2 个解决方案的错误。我的代码不是很漂亮和可读,但我认为它足够简单,可以理解这里的逻辑。我在这个问题上坐了一个小时,甚至制定了不同的解决方案,但在这个问题上找不到问题。
private static int _year = 1900;
private static int[] _months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
private static int _month = 0;
private static int _day = 7; //we start from Sunday
private static int counter = 0;
public static void main(String[] args) {
while(_year !=1901)
nextSunday(_day, _month, _year);
while(true) {
if(_year == 2000 && _month == 11 && _day > 31) break;
nextSunday(_day, _month, _year);
}
System.out.println(counter);
}
private static void nextSunday(int day, int month, int year) {
if(isSundayFirst(_day)) counter++;
if(year == 2000 && month == 11 && day + 7 > _months[month]) { //to break loop
_day = day + 7;
} else if(month == 11 && day + 7 > _months[month]) { // new year, end of month
_day = day + 7 - _months[month];
_month = 0;
_year++;
} else if(month == 1 && isLeapYear(year) && day + 7 > 29) { // leap year end of february
_day = day + 7 - 29;
_month++;
} else if(month == 1 && !isLeapYear(year) && day + 7 > _months[month]) { // not leap year end of february
_day = day + 7 - _months[month];
_month++;
} else if(day + 7 > _months[month]) { // end of month
_day = day + 7 - _months[month];
_month++;
} else { // next in same month
_day = day + 7;
}
}
private static boolean isSundayFirst(int day) {
return day == 1;
}
private static boolean isLeapYear(int year) {
if(isCentury(year)) {
if(year % 400 == 0) return true;
} else {
return year % 4 == 0;
}
return false;
}
private static boolean isCentury(int year) {
return year % 100 == 0;
}
我有 169 个这样的星期天。我应该再得到 2 个。
问题是:
You are given the following information, but you may prefer to do some research for yourself.
1 Jan 1900 was a Monday. Thirty days has September, April, June and November. All the rest have thirty-one, Saving February alone, Which has twenty-eight, rain or shine. And on leap years, twenty-nine. A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
我会感激任何努力。谢谢。
PS 我知道这个 _name
风格不像 Java,但我写得很快,并不打算将它发布到任何地方。
最佳答案
您的代码有两个问题,一个导致您多计 2,另一个导致您少计 4。
问题 #1 to 的问题告诉你从 1901 年开始数数,但你从 1900 年开始数数。
改变
if(isSundayFirst(_day)) counter++;
到
if(isSundayFirst(_day) && _year >= 1901) counter++;
解决这个问题。
问题 #2 问题出在这种情况下:
else if(day + 7 > _months[month]) { // end of month _day = day + 7 - _months[month]; _month++; }
你已经处理了前两个条件中是二月的情况,所以你需要检查以确保这里不是二月。将条件更改为
else if(month != 1 && day + 7 > _months[month])
将解决您的问题。
旁注:您的 nextSunday 方法的结构使其很难破译,因此我努力简化它(您现在将在 _year > 2000
时中断) :
private static void nextSunday(int day, int month, int year) {
int febNum = isLeapYear(_year) ? 29 : 28;
int dayCount = (month == 1) ? febNum : _months[_month]; //days in current month
_day += 7;
if (_day > dayCount) { //new month
_month++;
_day -= dayCount;
}
if (_month == 12) { //new year
_month = 0;
_year++;
}
}
关于java - 欧拉计划 #19 - 少了 2 个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43443891/