有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java项目Euler#19 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个这样的星期天。我应该再买两个

问题是:

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,但我写得很快,不想把它贴到任何地方


共 (1) 个答案

  1. # 1 楼答案

    你的代码有两个问题,一个导致你数到2,另一个导致你数到4

    1. 问题#1问题告诉你从1901年开始计数,但你从1900年开始计数

      改变

      if(isSundayFirst(_day)) counter++;
      

      if(isSundayFirst(_day) && _year >= 1901) counter++;
      

      来解决这个问题

    2. 问题#2问题在于这种情况:

      else if(day + 7 > _months[month]) { // end of month
          _day = day + 7 - _months[month];
          _month++;
      }
      

      在前两种情况中,您已经处理过2月份的情况,因此您需要检查以确保这里不是2月份。将条件更改为

      else if(month != 1 && day + 7 > _months[month]) 
      

      会解决你的问题

    旁注:你的下一个星期日方法的结构使它很难破译,因此我努力简化它(现在你将在^{时破译):

    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++;
        }
    }