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:\n'))
month = int(raw_input('month:\n'))
day = int(raw_input('day:\n'))
  
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.

python 实例四的更多相关文章

  1. python实例:解决经典扑克牌游戏 -- 四张牌凑24点 (二)

    Hey! 如果你还没有看这篇的上文的话,可以去稍稍瞅一眼,会帮助加速理解这一篇里面涉及到的递归结构哦!(上一篇点这里:<python实例:解决经典扑克牌游戏 -- 四张牌凑24点 (一)> ...

  2. Python 基础 四 面向对象杂谈

    Python 基础  四  面向对象杂谈 一.isinstance(obj,cls) 与issubcalss(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls ...

  3. 【Python实例一】使用minidom读取xml文件

    前言:最近刚在廖雪峰老师的网站里学习了Python的基础内容,想着循序渐进地找点实例练练手,网上看到有很多相关资料,决定针对感兴趣的内容实际编码实践一下,昨天刚好看到有关使用Python来读取XML文 ...

  4. 初识Python(四)

    一.数字数据类型 Python的数字数据类型用于存储数值,它是不可变的数据类型,这意味着改变数字数据类型,则需要一个新分配的对象: Python支持四种不同的数值类型: 整型(Int):通常被称为是整 ...

  5. 笨办法学python 第四版 中文pdf高清版|网盘下载内附提取码

    笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机了解不多,没有学过编程,但对编程感兴趣的朋友学习使用.这本书以习题的方式引导读者一步一步学习编 程,从简单的打印一 ...

  6. 第一个python实例--监控cpu

    #第一个python实例:监控cpu #/bin/bash/env Python from __future__ import print_function from collections impo ...

  7. (转)Python实例手册

    原文地址:http://hi.baidu.com/quanzhou722/item/cf4471f8e23d3149932af2a7 实在是太好的资料了,不得不转 python实例手册 #encodi ...

  8. 使用docker安装部署Spark集群来训练CNN(含Python实例)

    使用docker安装部署Spark集群来训练CNN(含Python实例) http://blog.csdn.net/cyh_24/article/details/49683221 实验室有4台神服务器 ...

  9. C语言库函数大全及应用实例四

    原文:C语言库函数大全及应用实例四                                    [编程资料]C语言库函数大全及应用实例四 couble fmod (double x, dou ...

随机推荐

  1. zuul网关入门(一、网关具有的功能)

    1. zuul网关入门(一.网关具有的功能) 1.1. 基本场景 1.1.1. API网关的由来 1.1.2. API网关基本功能 1.2. 高级应用 1.2.1. 亮点 可动态发布的过滤器机制 1. ...

  2. python之高阶函数和匿名函数

    map() map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回. def func(x): return ...

  3. Kubernetes系列之理解K8s Service的几种模式

    今天给大家介绍下k8s的service的几种访问模式. 概述 我们知道pod的ip不是固定的,是根据所在宿主机的docker0网卡生成的,每次重启,更新,调度等情况IP都会变,那pod与pod之间需要 ...

  4. ThinkPHP 数据库操作(七) : 视图查询、子查询、原生查询

    视图查询 视图查询可以实现不依赖数据库视图的多表查询,并不需要数据库支持视图,例如: Db::view('User','id,name') ->view('Profile','truename, ...

  5. BBS论坛(十九)

    19.1.cms轮播图管理页面布局 (1)cms/cms_base.html <li class="nav-group banner-manage"><a hre ...

  6. python中的pip

    python中的pip python有两个著名的包管理工具,其中,pip是一个.它对python的包进行管理和升级等操作. 问题一:pip本地的模块安装在哪里? 使用pip install numpy ...

  7. Vim 复制粘帖格式错乱问题的解决办法

    有时候,复制文本(尤其是代码)到 Vim,会出现格式错乱的问题.看样子,应该是自动缩进惹得祸.本文不去深究原因,直接给出解决方法. 1. paste 模式 运行如下命令,进入 paste 模式: :s ...

  8. 带着萌新看springboot源码13(手写一个自己的starter)

    springboot的最强大的就是那些xxxAutoconfiguration,但是这些xxxAutoConfiguration又依赖那些starter,只有导入了这些场景启动器(starter),我 ...

  9. Chapter 5 Blood Type——2

    The rest of the morning passed in a blur. 早上剩下的时间都在模糊中度过了. It was difficult to believe that I hadn't ...

  10. 从0到1,了解NLP中的文本相似度

    本文由云+社区发表 作者:netkiddy 导语 AI在2018年应该是互联网界最火的名词,没有之一.时间来到了9102年,也是项目相关,涉及到了一些AI写作相关的功能,为客户生成一些素材文章.但是, ...