zoukankan      html  css  js  c++  java
  • python 实例四

    https://www.cnblogs.com/evablogs/p/6754981.html

    题目:输入某年某月某日,判断这一天是这一年的第几天?

    程序分析:

    月份天数:

    月份 天数
    2 平年28天,闰年29天
    1,3,5,7,8,10,12 31
    4,6,9,11 30

     

     闰年:

    1、非整百年:能被4整除的为闰年。(如2004年就是闰年,2100年不是闰年)
     
    2、整百年:能被400整除的是闰年。(如2000年是闰年,1900年不是闰年)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    >>> L=[31,31,30,31,30,31,31,30,31,30,31]
    >>> def caldate(a,b,c):
        s=0
        if(a%100!=0 and a%4==0 or a%100==0 and a%400==0):
            L.insert(1,29)
        else:
            L.insert(1,28)
        for in range(b-1):
            s=s+L[i]
        return s+c
     
    >>> caldate(2016,1,2)
    2
    >>> caldate(2016,3,2)
    62

    改进版:考虑了月份和天数的有效性(哈哈,对比网上的答案,一看自己的代码就像是菜鸟级的,还有很多需要学习的地方)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    def caldate(a,b,c):
        L=[31,31,30,31,30,31,31,30,31,30,31]
        s=0
        Leap=0
        if(a%100!=0 and a%4==0 or a%100==0 and a%400==0):
            L.insert(1,29)
            Leap=1
        else:
            L.insert(1,28)
        if 0<b<=12:
            if 0<c<=31:
                if((b==2)and(Leap==1)and(c<=29)or((b==2)and(Leap==0)and(c<=28))):
                    for in range(b-1):
                        s=s+L[i]
                        return s+c
                else:
                    print 'The February should not greater than 28 or 29'
            else:
                print 'The date is error'
        else:
            print 'The month is invalid'

    输出:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    >>> caldate(2016,4,33)
    The date is error
    >>> caldate(2017,2,30)
    The February should not greater than 28 or 29
    >>> caldate(2017,2,28)
    59
    >>> caldate(2017,2,29)
    The February should not greater than 28 or 29
    >>> caldate(2017,13,29)
    The month is invalid

    网上答案:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
      
    year = int(raw_input('year: '))
    month = int(raw_input('month: '))
    day = int(raw_input('day: '))
      
    months = (0,31,59,90,120,151,181,212,243,273,304,334)
    if 0 < month <= 12:
        sum = months[month - 1]
    else:
        print 'data error'
    sum += day
    leap = 0
    if (year % 400 == 0or ((year % 4 == 0and (year % 100 != 0)):
        leap = 1
    if (leap == 1and (month > 2):
        sum += 1
    print 'it is the %dth day.' % sum

     输出结果:

    1
    2
    3
    4
    5
    6
    7
    year:
    2015
    month:
    6
    day:
    7
    it is the 158th day.
  • 相关阅读:
    使用 EasyBCD 安装Ubuntu 14.04 Error 15: file not found错误的解决方法
    浅谈程序猿的职业规划,看你如何决定自己的未来吧。
    [转载]DOS循环:bat/批处理for命令详解 (史上虽详尽的总结和说明~~)
    bat 批处理 字符串 截取
    window上使用GIT的个人经验(入门级)
    Android 访问 wcf
    解决 MyEclipse 10 中 JSp页面 “return false” 报错问题
    微信公共平台(码农在努力)
    Spring Cloud 中使用 Zipkin 追踪服务
    Spring Cloud Config 分布式配置中心
  • 原文地址:https://www.cnblogs.com/yuer20180726/p/10790239.html
Copyright © 2011-2022 走看看