Euler 19项目。星期天太多了

2024-09-27 00:11:31 发布

您现在位置:Python中文网/ 问答频道 /正文

来自Euler: 1900年1月1日是星期一。在二十世纪(1901年1月1日至2000年12月31日)每月的第一天有多少个星期日?

我的第一个功能是从已知的星期一开始,到2000年12月31日止,每天收集一份清单。第二个函数每七天保存一个星期日列表,从1901年的第一个星期日开始。在

def dayListBuild():
    dayListBuild = [[0,0,0]]
    for year in range(1900, 2001):
        for month in range(1, 13):
            if month in [4, 6, 9, 11]:
                dayRange = 30
            elif not month - 2:
                if year % 4 and (not year % 100 or year % 400):
                        dayRange = 29
                else:
                    dayRange = 28
            else:
                dayRange = 31
            for day in range(1, dayRange + 1):
                dayListBuild.append([year, month, day])
    return dayListBuild

def sundayList():
    dayList = dayListBuild()
    sundayList = []
    for day in range(len(dayList)):
        if not (day - 6) % 7 and dayList[day][0] > 1900 and not dayList[day][2] - 1:
            sundayList.append(dayList[day])
    return len(sundayList)

print(sundayList())

产量:200。 我的答案在29日前取消。答案是171。 这是我的星期天名单:

^{pr2}$

我不知道我做错了什么。在


Tags: andinforifdefnotrangeyear
2条回答

我只是想分享一下我对这个问题的代码,希望它能帮助到别人,或者更好,但有人可能会批评它。在

我知道有很多打印或者一些人认为不必要的打印正在进行,这有助于我理解控制台,所以如果你要对此发表评论,请省省口气。在

public class CountingSundays 
{
    enum days {Thursday, Friday, Saturday, Sunday, Monday, Tuesday,     Wednesday};

    private int totalNumberDays, // help to carry the days
                daysOfCurrentMonth, // check day of month
                month, // track month
                year, // track year
                firstSundays; // count the number of first sundays

    public static void main(String [] args)
    {
        // create an instance of CountingSundays
        CountingSundays cs = new CountingSundays();

        cs.year = 1901; // set the year
        cs.totalNumberDays = 1; // set the day

        while(cs.year < 2001)
        {
            // beginning of the year
            cs.month = 1; // reset the months

            // 30 days, Sept, April, June, Nov
            // 31 days, Jan, Mar, May, Jul, Aug, Oct, Dec
            // year % 4 == 0 and not 2000 as 2000 % 400 == 0,
            // feb is 29, else its 28

            while(cs.month < 13)
            {
                // beginning a new month
                cs.daysOfCurrentMonth = 1;
                // track the number of days per month
                int monthlyDayCutOff = 1; 

                if( cs.month == 4 | // Apr
                    cs.month == 6 | // Jun
                    cs.month == 9 | // Sep
                    cs.month == 11 ) // Nov

                         monthlyDayCutOff = 30; // these months have 30
                // if
                else if(cs.month == 2)
                {
                    // this is feb, check if it is a leap year
                    if(cs.year % 4 == 0 && 
                       cs.year % 100 != 0 ||
                       cs.year % 400 == 0)

                        monthlyDayCutOff = 29; // leap year

                    else
                        monthlyDayCutOff = 28; // not a leap year
                } // else if

                else

                    monthlyDayCutOff = 31; // one of the remaining months
                // else
                // use a while loop to calculate the days
                while(cs.daysOfCurrentMonth < monthlyDayCutOff)
                {
                    if(cs.daysOfCurrentMonth == 1 &
                       days.values()[cs.totalNumberDays % 7] == days.Sunday)
                    {
                        cs.firstSundays++; // increment first sundays
                        System.out.println("There is a first sunday in " + 
                                cs.month);

                    } // if

                    // print current month date, and day
                    // the modulus helps cycle thru our enum
                    System.out.println(cs.daysOfCurrentMonth + " " + 
                    days.values()[cs.totalNumberDays % 7]);

                    // print some statements
                    cs.daysOfCurrentMonth++; // increment days of month
                    cs.totalNumberDays++; // increment the total number of days

                } // while days

                // end of month
                cs.month++;

                System.out.println("\n\n" + cs.month);
            } // while month

            cs.year++;
        } // while year

        cs.year++;
        // print money shot
        System.out.println("Num of 1st sundays: " + cs.firstSundays);
    } // main

} // CountingSundays

你没有正确地使用模数。尝试:

if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):

相关问题 更多 >

    热门问题