The month's days(leap year defination)

leap year:

1.  能被400整除的是闰年

2. 不能被100整除但能被4整除的是闰年

 

if a year is divisible by 4 and not divisible by 100 or divisible by 400,it is a leap year

 

Given a year and month, return the days of that month.
 
class Solution:
    """
    @param year: a number year
    @param month: a number month
    @return: return the number of days of the month.
    """
    def get_the_month_days(self, year, month):
        # write your code here
        # 给定年份和月数,算出这个月有多少天
        # 除了闰年的2月份,每个月的天数都是固定的
        day = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        if month == 2:
            # 能被400整除的是闰年
            if year % 400 == 0:
                return 29
            # 不能被100整除但能被4整除的是闰年
            if year % 4 == 0 and year % 100 != 0:
                return 29
        
        return day[month]
            
            

 

posted @ 2022-05-10 22:48  7aughing  阅读(41)  评论(0编辑  收藏  举报