Python练习题 046:Project Euler 019:每月1日是星期天
本题来自 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日是星期天的更多相关文章
- Python练习题 032:Project Euler 004:最大的回文积
本题来自 Project Euler 第4题:https://projecteuler.net/problem=4 # Project Euler: Problem 4: Largest palind ...
- Python练习题 029:Project Euler 001:3和5的倍数
开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...
- Python练习题 039:Project Euler 011:网格中4个数字的最大乘积
本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...
- Python练习题 033:Project Euler 005:最小公倍数
本题来自 Project Euler 第5题:https://projecteuler.net/problem=5 # Project Euler: Problem 5: Smallest multi ...
- Python练习题 049:Project Euler 022:姓名分值
本题来自 Project Euler 第22题:https://projecteuler.net/problem=22 ''' Project Euler: Problem 22: Names sco ...
- Python练习题 048:Project Euler 021:10000以内所有亲和数之和
本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...
- Python练习题 047:Project Euler 020:阶乘结果各数字之和
本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial ...
- Python练习题 045:Project Euler 017:数字英文表达的字符数累加
本题来自 Project Euler 第17题:https://projecteuler.net/problem=17 ''' Project Euler 17: Number letter coun ...
- Python练习题 044:Project Euler 016:乘方结果各个数值之和
本题来自 Project Euler 第16题:https://projecteuler.net/problem=16 ''' Project Euler 16: Power digit sum 2* ...
随机推荐
- HttpWatch汉化版带详细的使用教程下载
http://www.wocaoseo.com/thread-303-1-1.html HttpWatch是强大的网页数据分析工具.集成在Internet Explorer工具栏.包括网页摘要.Coo ...
- IntelliJ IDEA远程Debug Linux的Java程序,找问题不要只会看日志了
1 前言 欢迎访问南瓜慢说 www.pkslow.com获取更多精彩文章! 我们习惯于在本地开发的时候debug,能快速定位与解决问题,那部署在服务器上是不是就没有办法了呢?只能通过查看日志来定位? ...
- [PyTorch 学习笔记] 5.1 TensorBoard 介绍
本章代码: https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson5/tensorboard_methods.py http ...
- Go语言 | goroutine不只有基础的用法,还有这些你不知道的操作
今天是golang专题第15篇文章,我们来继续聊聊channel的使用. 在我们的上篇文章当中我们简单介绍了golang当中channel的使用方法,channel是golang当中一个非常重要的设计 ...
- js map数据结构
Map 对象保存键值对,并且能够记住键的原始插入顺序.任何值(对象或者原始值) 都可以作为一个键或一个值. map对象常用于保存键值对,它的键是任意数据类型,常用于建立数据的映射关系 和对象的区别: ...
- 使用java8的方法引用替换硬编码
背景 想必大家在项目中都有遇到把一个列表的多个字段累加求和的情况,也就是一个列表的总计.有的童鞋问,这个不是给前端做的吗?后端不是只需要把列表返回就行了嘛...没错,我也是这样想的,但是在一场和前端的 ...
- Html中让输入框input和紧接在后的按钮button在垂直方向上对齐
<table border="0px" width="360px"> <tr><td colspan="10" ...
- springboot x.x.x RELEASE不同版本的差异
springboot 1.x.x RELEASE的 application.properties配置 server.context-path=/ server.port=8080 server.ses ...
- python基础:内置函数zip,map,filter
一.zip zip,就是把俩list,合并到一起,如果想同时循环2个list的时候,可以用zip,会帮你轮流循环两个list 比如: l1=[1,2,3,4,5] l2=['a','b','c','d ...
- JumpServer 架构浅解
Jumpserver 是一款由python编写开源的跳板机(堡垒机)系统,实现了跳板机应有的功能.基于ssh协议来管理,客户端无需安装agent.完全开源,GPL授权 设计思路 设计一个跳转网关,所有 ...