5#月份天数

题目

给定年份和月份,返回这个月的天数。
闰年的条件是:

能被 4 整除,但不能被 100 整除的年份
能被 400 整除的年份

思路

1.先判断闰年平年都一致的月份
2.分别判断是平年的2月还是闰年的2月

代码

    def getTheMonthDays(self, year, month):
        # write your code here
        #先判断闰年平年都一致的月份
        if month in [1,3,5,7,8,10,12]:
            return 31
        elif month in [4,6,9,11]:
            return 30
        #分别判断是平年的2月还是闰年的2月
        else:
            if(year%4==0 and year%100 !=0)or year%400==0:
                return 29
            else:
                return 28
posted @ 2024-02-24 20:20  荒坂株式会社  阅读(28)  评论(0)    收藏  举报