欧拉计划 发表于 2023-8-10 00:44:56

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

题目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 日)一共有多少个星期日落在了当月的第一天?


视频讲解:

https://www.bilibili.com/video/BV1nz4y1K7ep/


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

**** Hidden Message *****


歌者文明清理员 发表于 2023-8-10 00:50:00

本帖最后由 歌者文明清理员 于 2023-8-10 01:02 编辑

import datetime
count = 0
for y in range(1901, 2001):
    for m in range(1, 13):
      date = datetime.date(y, m, 1).weekday()
      if date == 0:
            count += 1
print(count)
print(sum(map(lambda x: __import__('datetime').date(*x, 1).weekday() == 0, __import__('itertools').product(range(1901, 2001), range(1, 13)))))

zhangjinxuan 发表于 2023-8-11 17:00:25

羡慕 python 的标准库{:10_266:}

欧拉计划 发表于 2023-8-11 23:44:26

zhangjinxuan 发表于 2023-8-11 17:00
羡慕 python 的标准库

哈哈,很多算法上的 “难题”,一来到 Python 上就不是问题了,既不用考虑溢出,又无需考虑类型的取值范围……

zhangjinxuan 发表于 2023-8-12 13:57:11

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

{:10_250:}

鱼C-小师妹 发表于 2023-8-30 11:04:50

{:10_279:}

nkysp 发表于 2023-9-1 08:38:48

Mr.roushan 发表于 2023-9-21 10:06:25

看看

这是豆豆啊 发表于 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)

hejiage 发表于 2024-1-4 19:35:51

一鼓作气倒19题,冲冲

kalendd 发表于 2024-2-24 18:44:40

{:9_231:}
页: [1]
查看完整版本: 题目19:20世纪有多少个星期日是当月的第一天?