1

次の情報が提供されますが、自分で調査することをお勧めします。

1900 年 1 月 1 日は月曜日でした。30 日間には、9 月、4 月、6 月、11 月があります。残りはすべて 31 で、2 月だけを保存します。これには 28 があります。そしてうるう年には、29。閏年は 4 で割り切れる年には発生しますが、400 で割り切れない限り、1 世紀には発生しません。

私は 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);
}
4

2 に答える 2

10

あなたは間違った日に始めています。1901 年 1 月 1 日は実際には火曜日です (currentDay = 2つまり .) その変更を行うと 171 になります: http://ideone.com/mh4MJ

于 2012-07-19T21:38:42.297 に答える
0

カレンダーを使用しないと、次のようになります。

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;
}
于 2015-08-14T22:34:26.647 に答える