首页 > 试题广场 >

计算一年中的第几天

[编程题]计算一年中的第几天
  • 热度指数:6353 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解

今年的第几天?

输入年、月、日,计算该天是本年的第几天。


输入描述:
包括三个整数年(1<=Y<=3000)、月(1<=M<=12)、日(1<=D<=31)。


输出描述:
输入可能有多组测试数据,对于每一组测试数据,
输出一个整数,代表Input中的年、月、日对应本年的第几天。
示例1

输入

1990 9 20
2000 5 1

输出

263
122
import sys

M = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
for line in sys.stdin:
    y, m, d = map(int, line.split())
    if (y % 4 == 0 and y % 10 != 0) + y % 400 == 0:
        M[1] += 1
    print(sum(M[0 : m - 1]) + d)

发表于 2026-01-24 22:10:06 回复(0)
def is_runyear(x):
    if (x%4==0 and x%100!=0) or x%400==0:
        return True
    else:
        return False

days_of_month = [31,28,31,30,31,30,31,31,30,31,30,31]
while True:
    try:
        Y,M,D = map(int,input().split())
        if M==1:
            answer = D
        elif M==2:
            answer = 31+D
        else:
            if is_runyear(Y):
                days_of_month[1] = 29
            answer = sum(days_of_month[0:M-1])+D
        print(answer)
    except:
        break
发表于 2025-08-24 22:39:40 回复(0)
import sys
month_day = [31,59,90,120,151,181,212,243,273,304,334,365]
for line in sys.stdin:
    y,m,d = map(int,line.split())

    # 平年天数
    days = month_day[m-2] + d

    # 闰年天数,如大于2月则在平年天数上加1天
    if y % 4 == 0 and y % 100 != 0&nbs***bsp;y % 400 == 0:
        if m > 2:
            days += 1

    print(days)

发表于 2025-07-29 20:28:43 回复(0)