根据输入的日期,计算是这一年的第几天。
保证年份为4位数且日期合法。
进阶:时间复杂度:,空间复杂度:
# 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)
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
#润年366天,第二月29天,能被4整除,1,3,5,7,8,10,12有31天,4,6,9,11有30天,普通年份是365天 while 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
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
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: breakdatatime支持日期加减,当前减去上一年最后一天
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())
# 方法一:一行代码实现(通过调用现成的方法) 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年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
# 计算当天与当年的第一天的间隔天数 # 如果是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
# 计算日期, 考虑二月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
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
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
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