zoukankan      html  css  js  c++  java
  • 2、求今天是第几天

    # coding:utf8
    
    import time
    
    #解法一
    def leapyear(n):
        return True if (n % 4 == 0 and n % 100 != 0) or n % 400 == 0 else False
    
    
    days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30]
    year, month, day = [int(x) for x in input('input year/month/day: ').split('/')]
    day2 = sum(days[:month - 1]) + day
    if leapyear(year) and month > 2:
        day2 += 1
    print(day2)

    解法二

    # coding:utf8
    
    import time
    
    
    def isValidDate(data_):
        try:
            time.strptime(data_, "%Y-%m-%d")
            return True
        except :
            return False
    
    def isLeapYear(year):
        if year % 400 == 0:
            return True
        elif year % 4 == 0 and year % 100 != 0:
            return True
        else:
            return False
    
    def get_today():
        float_num = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        data_ = input("请输入日期(格式为yyyy-mm-dd):")
        if isValidDate(data_):
            year, month, day = [int(x)] for x in data_.split('-')]
            if isLeapYear(year):
                float_num[1] = 29
    
            what_num = sum(float_num[0:month-1]) + day
            print("%s 是%d年的第%d天。" %(data_, year, what_num))
        else:
            print("输入的日期格式错误!")
    
    
    get_today()
    

      

    # coding:utf8
    import time

    def leapyear(n):    return True if (n % 4 == 0 and n % 100 != 0) or n % 400 == 0 else False

    days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30]year, month, day = [int(x) for x in input('input year/month/day: ').split('/')]day2 = sum(days[:month - 1]) + dayif leapyear(year) and month > 2:    day2 += 1print(day2)

    def isValidDate(data_):    try:        time.strptime(data_, "%Y-%m-%d")        return True    except :        return False
    def isLeapYear(year):    if year % 400 == 0:        return True    elif year % 4 == 0 and year % 100 != 0:        return True    else:        return False
    def get_today():    float_num = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]    data_ = input("请输入日期(格式为yyyy-mm-dd):")    if isValidDate(data_):        year, month, day = [int(x)] for x in data_.split('-')]        if isLeapYear(year):            float_num[1] = 29
            what_num = sum(float_num[0:month-1]) + day        print("%s 是%d年的第%d天。" %(data_, year, what_num))    else:        print("输入的日期格式错误!")

    get_today()

  • 相关阅读:
    [Leetcode] Set Matrix Zeroes
    [Leetcode] Longest Valid Parentheses
    [Leetcode] Interleaving String
    [Leetcode] Surrounded Regions
    [Leetcode] Candy
    用Delphi获取当前系统时间
    Delphi窗体中禁用最大化按钮
    DELPHI关于文件的操作
    Delphi 2010初体验,是时候抛弃Delphi 7了
    双通道内存有什么优点和缺点?
  • 原文地址:https://www.cnblogs.com/yahutiaotiao/p/12749722.html
Copyright © 2011-2022 走看看