本题来自 Project Euler 第19题:https://projecteuler.net/problem=19

'''
How many Sundays fell on the first of the month
during the twentieth century (1 Jan 1901 to 31 Dec 2000)? Answer: 171
''' from datetime import * firstDay = date(1901,1,1)
lastDay = date(2000,12,31) delta1 = timedelta(days=1)
firstSunday = date(1900,1,1)
while firstSunday == date(1900,1,1):
if firstDay.isoweekday() == 7:
firstSunday = firstDay #找出第1个星期天
break
else:
firstDay += delta1 delta7 = timedelta(days=7)
sundayCount = 0 #星期天的数量
while firstSunday <= lastDay:
if firstSunday.day == 1: #若为每月的1日
sundayCount += 1
firstSunday += delta7 #7天7天递增
print(sundayCount)

好吧,欧拉计划第18题做不出来,先跳过,先做第19题吧。

这题思路挺简单:在区间之内,先找出第1个星期天,然后7天7天地找,只要是每月的第1天,计数器就加1,很快就有答案。

话说,Python好像不能像MySQL那样,直接拿一个日期加上数字n,计算n天之后的日期;而是得借助timedelta,设定间隔时间,再进行运算,感觉……挺麻烦的。不过,用isoweekday()直接判断星期几、用day()直接判断是每月的第几天,倒是挺方便的~~~

Python练习题 046:Project Euler 019:每月1日是星期天的更多相关文章

  1. Python练习题 032:Project Euler 004:最大的回文积

    本题来自 Project Euler 第4题:https://projecteuler.net/problem=4 # Project Euler: Problem 4: Largest palind ...

  2. Python练习题 029:Project Euler 001:3和5的倍数

    开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...

  3. Python练习题 039:Project Euler 011:网格中4个数字的最大乘积

    本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...

  4. Python练习题 033:Project Euler 005:最小公倍数

    本题来自 Project Euler 第5题:https://projecteuler.net/problem=5 # Project Euler: Problem 5: Smallest multi ...

  5. Python练习题 049:Project Euler 022:姓名分值

    本题来自 Project Euler 第22题:https://projecteuler.net/problem=22 ''' Project Euler: Problem 22: Names sco ...

  6. Python练习题 048:Project Euler 021:10000以内所有亲和数之和

    本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...

  7. Python练习题 047:Project Euler 020:阶乘结果各数字之和

    本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial ...

  8. Python练习题 045:Project Euler 017:数字英文表达的字符数累加

    本题来自 Project Euler 第17题:https://projecteuler.net/problem=17 ''' Project Euler 17: Number letter coun ...

  9. Python练习题 044:Project Euler 016:乘方结果各个数值之和

    本题来自 Project Euler 第16题:https://projecteuler.net/problem=16 ''' Project Euler 16: Power digit sum 2* ...

随机推荐

  1. websocket学习(转载)

    public interface WebSocketMessageBrokerConfigurer { // 添加这个Endpoint,这样在网页中就可以通过websocket连接上服务,也就是我们配 ...

  2. centos6.8上安装部署 jhipster-registry

    必备环境:jdk8,git,maven 1.安装nodejs #由于采用编译的方式很容易出现一些意外的惊喜,所以我们这儿直接用yum命令安装 #1.查看nodejs版本(命令中不要加 -y 如果版本不 ...

  3. akka-grpc - 应用案例

    上期说道:http/2还属于一种不算普及的技术协议,可能目前只适合用于内部系统集成,现在开始大面积介入可能为时尚早.不过有些项目需求不等人,需要使用这项技术,所以研究了一下akka-grpc,写了一篇 ...

  4. EventDispatcher

    事件分发类,提供事件注册.移除.触发功能 采用delegate.dictionary实现 支持自定义事件.事件采用字符串方式标识 支持 0,1,2,3,4 等5种不同参数个数的回调函数   // 路由 ...

  5. Photogrammetry and Game

    https://skulltheatre.wordpress.com/2013/02/11/photogrammetry-in-video-games-frequently-asked-questio ...

  6. 【HttpRunner v3.x】笔记—8.运行testcase的几种方式

    在之前的demo过程中,已经运行过testcase了,那这篇就也来汇总一下,运行case相关的知识点. 一.运行testcase的几种场景 1. 运行单个case 通常单个case的话我会在编辑器里用 ...

  7. RedisTemplate: Failed to deserialize payload

    问题 org.springframework.data.redis.serializer.SerializationException: Cannot deserialize; nested exce ...

  8. Java中枚举的用法

    public enum Week { DAY1("周一", 0.9), DAY2("周二", 0.9), DAY3("周三", 0.8), ...

  9. Spring IoC 到底是什么

    前言 「上一篇文章」我们对 Spring 有了初步的认识,而 Spring 全家桶中几乎所有组件都是依赖于 IoC 的. 刚开始听到 IoC,会觉得特别高大上,但其实掰开了很简单. 跟着我的脚步,一文 ...

  10. 安装python3,配置pycharm

    1.下载最新版python3 首先去python官网下载python3的源码包,网址:https://www.python.org/ 进去之后点击导航栏的Downloads,也可以鼠标放到Downlo ...