鱼C论坛

 找回密码
 立即注册
查看: 535|回复: 10

题目19:20世纪有多少个星期日是当月的第一天?

[复制链接]
发表于 2023-8-10 00:44:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
题目19:20世纪有多少个星期日是当月的第一天?


Counting Sundays

You are given the following information, but you may prefer to do some research for yourself.

  • 1 Jan 1900 was a Monday.
  • Thirty days has September,
    April, June and November.
    All the rest have thirty-one,
    Saving February alone,
    Which has twenty-eight, rain or shine.
    And on leap years, twenty-nine.
  • A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.

How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?


题目翻译:

以下是一些已知的信息,但是或许你需要自己做一些其他的调查。

  • 1900 年 1 月 1 日是星期一。
  • 30 天的月份有:4 月,6 月,9 月,11月。
  • 此外的月份都是 31 天,当然 2 月除外。
  • 2 月在闰年有 29 天,其他时候有 28 天。
  • 年份可以被 4 整除的时候是闰年,但是不能被 400 整除的世纪年(100 的整数倍年)除外。

20 世纪(1901 年 1 月 1 日到 2000 年 12 月 31 日)一共有多少个星期日落在了当月的第一天?


视频讲解:




思路解析及源码参考(C & Python):

游客,如果您要查看本帖隐藏内容请回复



想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-8-10 00:50:00 | 显示全部楼层
本帖最后由 歌者文明清理员 于 2023-8-10 01:02 编辑
  1. import datetime
  2. count = 0
  3. for y in range(1901, 2001):
  4.     for m in range(1, 13):
  5.         date = datetime.date(y, m, 1).weekday()
  6.         if date == 0:
  7.             count += 1
  8. print(count)
  9. print(sum(map(lambda x: __import__('datetime').date(*x, 1).weekday() == 0, __import__('itertools').product(range(1901, 2001), range(1, 13)))))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-11 17:00:25 | 显示全部楼层
羡慕 python 的标准库
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-8-11 23:44:26 | 显示全部楼层
zhangjinxuan 发表于 2023-8-11 17:00
羡慕 python 的标准库

哈哈,很多算法上的 “难题”,一来到 Python 上就不是问题了,既不用考虑溢出,又无需考虑类型的取值范围……
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-12 13:57:11 | 显示全部楼层
欧拉计划 发表于 2023-8-11 23:44
哈哈,很多算法上的 “难题”,一来到 Python 上就不是问题了,既不用考虑溢出,又无需考虑类型的取值范 ...

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-30 11:04:50 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-9-1 08:38:48 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-9-21 10:06:25 | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-10-27 17:33:53 | 显示全部楼层
import datetime

start_date = datetime.date(1901, 1, 1)
end_date = datetime.date(2000, 12, 1)

sundays_on_first = 0

current_date = start_date
while current_date < end_date:
    if current_date.weekday() == 6:  # Sunday is represented by 6 in Python's datetime module
        sundays_on_first += 1

    # Move to the first day of the next month
    if current_date.month == 12:
        current_date = datetime.date(current_date.year + 1, 1, 1)
    else:
        current_date = datetime.date(current_date.year, current_date.month + 1, 1)

print(sundays_on_first)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-1-4 19:35:51 | 显示全部楼层
一鼓作气倒19题,冲冲
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-24 18:44:40 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-22 10:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表