首页 > 试题广场 >

计算日期到天数转换

[编程题]计算日期到天数转换
  • 热度指数:162919 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
根据输入的日期,计算是这一年的第几天。
保证年份为4位数且日期合法。
进阶:时间复杂度:,空间复杂度:

输入描述:

输入一行,每行空格分割,分别是年,月,日



输出描述:
输出是这一年的第几天
示例1

输入

2012 12 31

输出

366
示例2

输入

1982 3 4

输出

63
python
# coding: utf-8
def is_loap_year(n):
    if n % 400 == 0&nbs***bsp;(n % 4 == 0 and n % 100 != 0):
        return True
    return False
def func(data):
    data_list = map(int,data.split())
    month_days = [31,28,31,30,31,30,31,31,30,31,30,31]
    if is_loap_year(data_list[0]):
        month_days[1] = 29
    out = 0
    for i in range(data_list[1]-1):
        out += month_days[i]
    print out+data_list[2]
if __name__ == "__main__":
    import sys
    line = sys.stdin.readline().strip()
    func(line)


发表于 2022-03-31 23:00:12 回复(0)
import sys

# 计算是否闰年
# 不能被4整除或者能被100整除,但是不能被400整除的是平年,其他是闰年
def func(year):
    if year%4 != 0&nbs***bsp;(year%100 == 0 and year%400 != 0):
        return False
    return True


for line in sys.stdin:
    try:
        num = 0
        mon_lst = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        year, mon, day = [int(i) for i in line.split(' ')]
        # 如果是闰年,2月份改成29年
        if func(year):
            mon_lst[1] = 29
        for i in range(mon-1):
            num += mon_lst[i]
        print(num+day)
    except Exception as ex:
        print(ex)
        pass

发表于 2021-05-01 11:35:14 回复(0)
#润年366天,第二月29天,能被4整除,1,3,5,7,8,10,1231天,4,6,9,1130,普通年份是365while True: try:
        year,month,day=map(int,input().split(' ')) if year<=0 or month<=0 or month>12 or day<=0 or day>31: print(-1) else:
            m=[31,29,31,30,31,30,31,31,30,31,30,31] if (year%400==0) or (year%4==0 and year%100!=0): print(sum(m[:(month-1)])+day) else: if month>2: print(sum(m[:(month-1)])+day-1) else: print(sum(m[:(month-1)])+day) except: break
发表于 2021-04-08 10:17:05 回复(0)
while True:
    try:
        year, month, day = map(int, input().split())
        leap = 0
        if (year % 4 == 0 and year % 100 != 0)&nbs***bsp;(year % 400 == 0):
            leap = 1
        reg_days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        leap_days = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        day_in_year = 0
        for i in range(1, month):
            if leap == 0:
                day_in_year += reg_days[i]
            elif leap == 1:
                day_in_year += leap_days[i]
        day_in_year += day
        print(day_in_year)
    except EOFError:
        break

发表于 2021-03-27 00:29:37 回复(0)
import datetime
while True:
    try:
        come = input().strip().split()
        targetDay = datetime.date(int(come[0]), int(come[1]), int(come[2]))
        dayCount = targetDay - datetime.date(int(come[0])- 1, 12, 31)  # 减去上一年最后一天
        print(dayCount.days)
    except:
        break
datatime支持日期加减,当前减去上一年最后一天
发表于 2021-02-17 15:57:41 回复(1)
#写的比较简单
def fun1(a,b,c):
    list1 = [1,3,5,7,8,10,12]
    sum1 = 0
    for i in range(1,b):
        if i in list1:
            sum1 = sum1+ 31
        
        elif i == 2 :
            if  a/4%2 == 0  and a/100%2 != 0 :
                
                sum1 = sum1 +29
            elif a/4%2 == 1  and a/100%2 != 0 :
                sum1 = sum1+29
            else:
                sum1 = sum1 + 28
        elif i not in list1:
            sum1 = sum1 +30
    
    sumtot = sum1 + c
    return sumtot
a,b,c = map(int,input().split())
print(fun1(a,b,c))
    

while True:
    try :
        a,b,c = map(int,input().split())
        print(fun1(a,b,c))
    except:
        break

    

发表于 2021-01-19 18:05:08 回复(0)
import traceback
import sys


monthes = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

def is_run(y):
    if y % 4:
        return False
    if y % 100:
        return True
    if y % 400:
        return False
    return True


def cal(y, m, d):
    days = sum(monthes[:m-1]) + d
    days = days+1 if is_run(y) and m>2 else days
    return days


try:
    for s in sys.stdin:
        y, m, d = [int(_) for _ in s.split()]
        print(cal(y, m, d))
except Exception as e:
    print(traceback.format_exc())

发表于 2020-12-13 01:21:58 回复(0)
# 方法一:一行代码实现(通过调用现成的方法)
import datetime
while True:
    try:
        print(datetime.datetime(*map(int, input().split())).strftime("%j").lstrip("0"))
    except:
        break

# 方法二:手动计算第几天,练习逻辑
# 注意:应该先对年份进行是否为闰年的判断,若为闰年,将Day的二月份天数更正为29,
#      以便后续在判断非法输入时,二月份所包含的天数为正确的。否则,2000 2 29无法通过。
def outDay(year, month, day):
    # 每个月所包含的天数
    days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    # 如果输入年份为闰年,则将第二月对应的天数改为29天。
    if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
        days[1] = 29
    # 判断输入的年月日是否合法
    if year <= 0 or month <= 0 or month > 12 or day <= 0 or day > days[month-1]:
        return -1
    # sum为前面month-1个月的天数总和
    sum = 0
    for i in range(month-1):
        sum += days[i]
    return sum + day

while True:
    try:
        year, month, day = map(int, input().strip().split(' '))
        res = outDay(year, month, day)
        print(res)
    except:
        break

编辑于 2020-12-09 20:42:36 回复(0)
# 2020年11月14日15:31:54
while True:
    try:
        days = 0
        year, month, day = map(int, input().split())
        for i in range(1, month):
            if i in [1, 3, 5, 7, 8, 10, 12]:
                days += 31
            elif i == 2:
#               判断瑞年
                if (year%400==0)&nbs***bsp;(year%4==0 and year%100!=0):
                    days += 29
                else:
                    days += 28
            else:
                days += 30
        days += day 
        print(days)
    except:
        break
        


编辑于 2020-11-14 15:59:18 回复(0)
# 计算当天与当年的第一天的间隔天数
# 如果是N,则当天为当年的N+1天
import datetime
while True:
    try:
        year, month, day = map(int, input().split())
        date_current = datetime.date(int(year), int(month), int(day))
        date_first = datetime.date(int(year), 1, 1)
        print((date_current - date_first).days + 1)
    except:
        break

发表于 2020-08-23 10:39:50 回复(0)
# 计算日期, 考虑二月28之前的日期,不能用全局闰年+1
def getOutDay(y, m, d):
    # 闰年1-12月分别为31天,29天,31天,30天,31天,30天,31天,31天,30天,31天,30天,31天
    months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] # 平年
    if (y//100 == y/100) and y//100 % 4 == 0:  # 尾数成百的年份要能被400整除才是闰年 :1900
        months[1] = 29
    elif y % 4 == 0 and (y//100 != y/100):     # 尾数不成百的年份 2012 
        months[1] = 29
    return sum(months[:m-1]) + d
    
while True:
    try:
        y, m, d = map(int, input().split())
        print(getOutDay(y, m, d))
    except:
        break

编辑于 2020-08-22 15:52:01 回复(0)
import datetime
while True:
    try:
        y,m,d = map(int, input().split())
        day = datetime.datetime(y, m, d)-datetime.datetime(y, 1, 1)
        print(int(day.days)+1)
    except:
        break

编辑于 2020-08-19 21:38:12 回复(0)
def f(y,m,d):
    if m == 1:
        return d
    else:
        count = 0
        for i in range(1,m+1):
            if i == m:
                count += d
            elif int(i) in [1,3,5,7,8,10,12]:
                count +=31
            elif int(i) in [4,6,9,11]:
                count += 30
            elif int(i) == 2:
                if int(y) % 4 == 0:
                    count += 29
                else:
                    count += 28
        return count


while True:
    try:
        a = input().split(' ')
        y,m,d = a[0],a[1],a[2]
        if y and m and d:
            print(f(int(y),int(m),int(d)))
    except:
        break

发表于 2020-08-05 16:05:41 回复(0)
小学生也能看懂的代码:
def count_days(year, month, day):
    days = 0
    for i in range(1, month):
        if i in [1, 3, 5, 7, 8, 10, 12]:
            days += 31
        elif i in [4, 6, 9, 11]:
            days += 30
        else:
            if (year % 4 == 0 and year % 100 != 0)&nbs***bsp;(year % 400 == 0):
                days += 29
            else:
                days += 28
    days += day
    return days


while True:
    try:
        year, month, day = list(map(int, input().split()))
        print(count_days(year, month, day))

    except:
        break



发表于 2020-08-05 12:21:46 回复(0)
应该是考虑各种非法输入的情况了

def caldate(y, m, d):
    date = {1:31, 2:28, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}
    ***, cntda = 0, 0
    if y%4 == 0 and y%100 != 0 :
        date[2] = 29
    if m not in date:
        return -1
    elif d > date[m]&nbs***bsp;d < 1:
        return -1
    else:
        for i in range(1, m):
            cntda += date[i]
    return cntda + d
while True:
    try:
        y, m, d = map(int, input().split())
        print(caldate(y, m, d))
    except:
        break


发表于 2020-07-20 17:58:38 回复(0)

问题信息

难度:
34条回答 31973浏览

热门推荐

通过挑战的用户

查看代码