【问题标题】:Project Euler #19 in Java (off by 1)Java 中的 Project Euler #19(减 1)
【发布时间】:2012-07-19 21:34:17
【问题描述】:

您将获得以下信息,但您可能更愿意自己做一些研究。

1900 年 1 月 1 日是星期一。 九月三十天, 四月、六月和十一月。 其余的都有三十一个, 独自拯救二月, 其中有二十八,风雨无阻。 在闰年,二十九。 闰年出现在任何能被 4 整除的年份,但不会出现在世纪上,除非它能被 400 整除。 在 20 世纪(1901 年 1 月 1 日至 2000 年 12 月 31 日)有多少个星期日?

我一直得到 172,答案是 171。我不确定可能是什么问题,我已经尝试了所有方法,但我一直得到 172。感谢您的帮助。

public static void main(String args[]){
    int year=1901;
    boolean isLeapYear=false;
    int totalSundays=0;
    int currentDay=1;//Starts on a Monday
    while(year<=2000){
        isLeapYear=false;
        if((year%4)==0){
            if((year%100)==0 && (year%400)==0){
                isLeapYear=true;
            } else if((year%100)==0 && (year%400)!=0){
                isLeapYear=false;
            } else {
                isLeapYear=true;
            }
        }
        System.out.println("The Year Is: "+year);
        System.out.println("*******************************");
        for(int month=1;month<=12;month++){
            System.out.println("The Month is: "+month+" currentDay is :  "+currentDay);
            if(currentDay==7){
                totalSundays++;
            }
            if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12){
            //January,March,May,July,August,October,December
                currentDay+=3;
            } else if(month==4 || month==6 || month==9 || month==11){ 
            //April,June,September,November
                currentDay+=2;              
            } else if(month==2 && isLeapYear){
            //February has 29 days in a Leap Year
                currentDay+=1;
            } 

            if(currentDay>7){
                currentDay=currentDay-7;
            }
            System.out.println("Updated Current Day Is :  "+currentDay);
        }
        System.out.println("*******************************");
        year++;
    }
    System.out.println("The total number of Sundays that fell in the first of the month is: "+totalSundays);
}

【问题讨论】:

  • 仅供参考,如果您消除了幻数并使用final ints,您/我们将更容易遵循和维护

标签: java calendar


【解决方案1】:

你从错误的一天开始。 1901 年 1 月 1 日实际上是一个星期二(所以 currentDay = 2。)进行更改会导致 171:http://ideone.com/mh4MJ

【讨论】:

  • 感谢您的帮助,是的,实际上它从星期二开始,因为 1900 年 1 月 1 日是星期一,而 1900 年有 365 天,所以 1901 年 1 月 1 日应该是星期二。
  • 有趣的是,有多少人本能地认为 1901 年 1 月 1 日是星期一而不是检查。
【解决方案2】:

不使用日历会是:

static int getDays(int month, int year){
    switch (month){
    case 4:case 6: case 9: case 11:
        return 30;
    case 2:
        return (isLeapYear(year))?29:28;
    }
    return 31;
}

static boolean isLeapYear(int year){
    boolean leap = false;
    if(year%4==0){
        if(year%100==0){
            leap = (year%400==0)?true:false;
        }
        else{
            leap = true;
        }
    }
    return leap;
}

static int q19(){
    int year = 1901;
    // Sun:1 Mon: 2 ...
    int firstDay = 3; // 1 Jan 1901 was Tuesday 
    int sundays = 0;
    for(int i=year;i<2001;i++){
        int days_In_Month = getDays(1,i);
        int dif = days_In_Month%7;
        if(i!=year)
            firstDay = (dif+firstDay)%7;
        if(firstDay == 1) sundays++;
        for(int m = 2;m<=12;m++){
            firstDay = (dif+firstDay)%7;
            if(firstDay == 1) sundays++;
            days_In_Month = getDays(m,i);
            dif = days_In_Month%7;
        }
    }
    return sundays;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多