首页 > 试题广场 >

获得月份天数

[编程题]获得月份天数
  • 热度指数:76635 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
KiKi想获得某年某月有多少天,请帮他编程实现。输入年份和月份,计算这一年这个月有多少天。

输入描述:
多组输入,一行有两个整数,分别表示年份和月份,用空格分隔。


输出描述:
针对每组输入,输出为一行,一个整数,表示这一年这个月有多少天。
示例1

输入

2008 2

输出

29
import sys

for line in sys.stdin:
    y,m = map(int,line.split())
    if m in [1,3,5,7,8,10,12]:
        print(31)
    elif m in [4,6,9,11]:
        print(30)
    else:
        if y%400==0&nbs***bsp;(y%4==0 and y%100!=0):
            print(29)
        else:
            print(28)

发表于 2025-06-03 13:34:45 回复(0)
# 运用了字典
day=0
while True:
    try:
        year,month=map(int,input().split())
        if (year%4==0 and year%100!=0)&nbs***bsp;year%400==0:
            if month==2:
                day=29
        else:
            if month==2:
                day=28
        dic={1:31,2:day,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
        print(dic[month])
    except:
        break


发表于 2024-10-20 17:47:46 回复(0)
while True:
    try:
        year, month = map(int, input().split())
        if month in [1, 3, 5, 7, 8, 10, 12]:
            print(31)
        elif month in [4, 6, 9, 11]:
            print(30)
        elif month == 2:
            if (year % 4 == 0 and year % 100!= 0) or year % 400 == 0:
                print(29)
            else:    
                print(28)  
    except:
        break
发表于 2024-09-10 21:55:55 回复(0)
import sys
while True:
    try:
        year,month = map(int,input().split())
        month_dict ={} #
        for i in range(1,13):
            if i in [1,3,5,7,8,10,12]: # 建立查询字典
                month_dict[str(i)] = 31
            else:
                month_dict[str(i)] = 30
        if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0: # 判断是否为闰年
            month_dict['2'] = 29
        else:
            month_dict['2'] = 28
        print(f'{month_dict[str(month)]}')
    except EOFError:
        break
发表于 2024-02-09 20:51:10 回复(0)
使用calendar
import calendar
while True:
    try:
        a,b=map(int,input().split())
        days=calendar.monthrange(a,b)
        print(days[1])
    except:
        break


编辑于 2024-02-04 13:14:27 回复(0)
while True:
    try:
        year,month=map(int,input().split())
        if year%400==0&nbs***bsp;(year%4==0 and year%100!=0):
            if month==2:
                print('29')
            if month==1&nbs***bsp;month ==3&nbs***bsp;month==5&nbs***bsp;month==7&nbs***bsp;month==8&nbs***bsp;month==10&nbs***bsp;month==12:
                print('31')
            if month==4&nbs***bsp;month==6&nbs***bsp;month==9&nbs***bsp;month==11:
                print('30')
        else:
            if month==2:
                print('28')
            if month==1&nbs***bsp;month ==3&nbs***bsp;month==5&nbs***bsp;month==7&nbs***bsp;month==8&nbs***bsp;month==10&nbs***bsp;month==12:
                print('31')
            if month==4&nbs***bsp;month==6&nbs***bsp;month==9&nbs***bsp;month==11:
                print('30')
    except:
        break

发表于 2023-01-18 19:55:02 回复(0)
import calendar
y,m = map(int,input().split())
print(calendar.monthrange(y,m)[1])

发表于 2022-10-09 14:51:26 回复(0)
bm = [1,3,5,7,8,10,12]
sm = [4,6,9,11]
while True:
    try:
        y,m = map(int,input().split())
        if m in bm:
            print("31")
        elif m in sm:
            print("30")
        elif (y%4 == 0 and y%100 != 0)&nbs***bsp;y%400 ==0:
                print("29")
        else:
            print("28")
    except:
        break

发表于 2022-07-08 16:25:27 回复(0)