首页 > 试题广场 >

今年的第几天?

[编程题]今年的第几天?
  • 热度指数:39485 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
输入年、月、日,计算该天是本年的第几天。

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


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

输入

1990 9 20
2000 5 1

输出

263
122
while True:
    try:
        year,month,day=map(int,input().strip().split(' '))
        def isrunnian(year):
            if (year%4==0 and year%100!=0) or year%400==0:
                return True
            else:
                return False
        time=[31,29,31,30,31,30,31,31,30,31,30,31]
        re=0
        if isrunnian(year):
            #print(time[:month])
            re+=sum(time[:month-1])+day
        else:
            if month>2:
                re+=sum(time[:month-1])+day-1
            else:
                re+=sum(time[:month-1])+day
        print(re)

    except:
        break
编辑于 2019-08-05 16:13:27 回复(0)

python三行解法:

import datetime
while True:
    try:
        a,b,c=map(int,input().split())
        dd=datetime.datetime(a,b,c)
        print(dd.strftime("%j").lstrip("0"))



    except:
        break

需要注意的是,输出的结果前面有可能带0,所以要把左边的0使用lstrip去掉。

于是,上面的代码可以转为一行:

import datetime
while True:
    try:

        print(datetime.datetime(*map(int,input().split())).strftime("%j").lstrip("0"))



    except:
        break

我就想问,还有哪种语言比这个更简单。。

发表于 2017-10-04 14:12:23 回复(6)
while 1:
    try:
        y,m,d=list(map(int,input().split()))
        m1=[31,29,31,30,31,30,31,31,30,31,30,31]
        m2=[31,28,31,30,31,30,31,31,30,31,30,31]
        if (y%4 == 0 and y%100 != 0) or (y%400 == 0):
            print(sum(m1[0:m-1])+d)
        else:
            print(sum(m2[0:m-1])+d)
    except:
        break

发表于 2017-09-02 15:33:37 回复(0)
import datetime
D = datetime.datetime
try:
    while 1:
        a = map(int, raw_input().split())
        print (D(a[0], a[1], a[2]) - D(a[0] - 1, 12, 31)).days
except:
    pass

发表于 2016-12-24 19:51:47 回复(0)