首页 > 试题广场 >

月份天数

[编程题]月份天数
  • 热度指数:1649 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
输入年份月份,请输出这个月的天数

输入描述:


多组输入输出
第一个参数为年份,如2018代表2018年,2019代表2019年
第二个参数为月份,如1代表1月,2代表2月
(参数间均以空格隔开)




输出描述:
输出当月的实际天数。
示例1

输入

2018 2
2020 2
2019 1

输出

28
29
31
def get_month_year(x, y):
    if y in [1, 3, 5, 7, 8, 10, 12]:
        return 31
    elif y in [4, 6, 9 ,11]:
        return 30
    elif y == 2 and x%4 == 0 and x%100 != 0&nbs***bsp;x%400 == 0:
        return 29
    else:
        return 28
while True:
    try:
        year, month = map(int,input().split())
        print(get_month_year(year, month))
    except:
        break;

发表于 2020-03-15 21:26:44 回复(2)
day=[31,28,31,30,31,30,31,31,30,31,30,31]
 
while True:
    try:
        year,month = map(int,input().split())
        if month==2:
            if (year%4==0 and year%100!=0) or (year%400==0):
                print(day[month-1]+1)
                continue
        print(day[month-1])
    except:
        break

编辑于 2020-11-07 16:29:40 回复(0)
此题考的就是什么是闰年什么是平年。。。。
dic = {'1':'31','2':['28','29'],'3':'31','4':'30','5':'31','6':'30',
      '7':'31','8':'31','9':'30','10':'31','11':'30','12':'31'}
while True:
    try:
        data = input().strip(' ').split(' ')
        if data[1] != '2':
            print(dic[data[1]])
        else:
            cur= int(data[0])
            
            if cur % 4 == 0 and cur % 100 != 0:
                print(dic[data[1]][1])
            elif cur % 100 == 0 and cur % 400 == 0:
                print(dic[data[1]][1])
            else:
                print(dic[data[1]][0])
    except:
        break

发表于 2020-08-21 23:08:30 回复(0)
其实只需要判断闰年就可以了
while True:
    try:
        year, month = map(int, input().split())
        if month in (1, 3, 5, 7, 8, 10, 12):
            print(31)
        if month in (4, 6, 9, 11):
            print(30)
        if month == 2:
            # 只需判断一下闰年即可
            if (year % 100 != 0 and year % 4 == 0)&nbs***bsp;(year % 400 == 0):
                print(29)
            else:
                print(28)
    except:
        break


发表于 2020-12-23 15:31:14 回复(0)
def judge_days(a,b):
    if b != 2:
        if b in (1,3,5,7,8,10,12):
            print(31)
        else:
            print(30)
    elif a%100 == 0:
        if a%400 == 0:
            print(29)
        else:
            print(28)
    elif a%4 == 0:
        print(29)
    else:
        print(28)

while True:
    try:
        year, month = map(lambda x:int(x),input().split(' '))
        judge_days(year, month)
    except:
        break

发表于 2020-05-11 23:19:46 回复(0)
#include <stdio.h>
int year(int y)
{
    return (((y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0)));
}
int main()
{
    int y = 0;
    int m = 0;
    int d = 0;
    int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

while(scanf("%d %d", &y,&m)==2)
{
    int d =days[m];
    if((year(y)==1)&&(m==2))
    {
    d++;
    }
    printf("%d\n",d);
}
    return 0;
}


编辑于 2023-08-24 12:35:47 回复(0)
import calendar as cld
t = input('请输入年月(YYYYMM):')
y = int(t[:4])
m = int(t[5:])
cld.monthrange(y,m)[1]


编辑于 2021-06-11 15:29:17 回复(0)
#输出月份天数
def daynum(x,y):
    if (x%400==0) or (x%100!=0 and x%4==0):
        if y in [1,3,5,7,8,10,12]:
            return 31
        elif y in [4,6,9,11]:
            return 30
        else:
            return 29
    else:
        if y in [1,3,5,7,8,10,12]:
            return 31
        elif y in [4,6,9,11]:
            return 30
        else:
            return 28
if __name__== '__main__':
    while True:
        try:
            x,y=map(int,input().split(" "))
            res=daynum(x,y)
            print(res)
        except:
            break

编辑于 2021-04-10 15:21:03 回复(0)
li=[]
while True:
    try:
        li.append(list(map(int, input().strip().split(' '))))
    except:
        break

leapyear=[31,29,31,30,31,30,31,31,30,31,30,31]
averageyear=[31,28,31,30,31,30,31,31,30,31,30,31]

for i in range(len(li)):
    if (li[i][0]%4==0 and li[i][0]%100!=0)&nbs***bsp;li[i][0]%400==0:
        print(leapyear[li[i][1]-1])
    else:
        print(averageyear[li[i][1]-1])

发表于 2020-05-13 22:49:20 回复(0)
#include <iostream>
 
using namespace std;
 
bool isLeap(int year) {
    if (year % 100 != 0) {
        return year % 4 == 0;
    }
    else {
        return year % 400 == 0;
    }
}
 
int main()
{
    int year, month;
    int day[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
    while (cin>> year >> month) {
        if (month != 2) {
            cout << day[month - 1] << endl;
        }
        else {
            if (isLeap(year)) {
                cout << 29 << endl;
            }
            else {
                cout << 28 << endl;
            }
        }
    }
     
    return 0;
}

发表于 2020-03-12 03:00:35 回复(0)