鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: 欧拉计划

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

[复制链接]
发表于 2022-6-8 16:02:43 | 显示全部楼层
  1. import time as t

  2. start = t.perf_counter()


  3. def leap_year(year):
  4.     if year % 4:
  5.         return 0
  6.     else:
  7.         if year % 100:
  8.             return 1
  9.         elif year % 400:
  10.             return 0
  11.         else:
  12.             return 1


  13. res, sundays = 1, 0
  14. dates, leap_years = [], []
  15. for i in range(1901, 2001):
  16.     is_leap = leap_year(i)
  17.     month_day = [3, 0 + (1 * is_leap), 3, 2, 3, 2, 3, 3, 2, 3, 2, 3]
  18.     count_month = 1
  19.     for month in month_day:
  20.         count_month += 1
  21.         res += month
  22.         if not ((res + 1) % 7):
  23.             sundays += 1
  24.             if count_month < 13:
  25.                 date = "%s.%s" % (i, count_month)
  26.                 dates.append(date)
  27.             else:
  28.                 date = "%s.%s" % (i + 1, 1)
  29.                 dates.append(date)

  30. for i in dates:
  31.     print(i)
  32. print(sundays)
  33. print("It costs %f s" % (t.perf_counter() - start))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-6-8 16:03:46 | 显示全部楼层
  1. import time as t

  2. start = t.perf_counter()


  3. def leap_year(year):
  4.     if year % 4:
  5.         return 0
  6.     else:
  7.         if year % 100:
  8.             return 1
  9.         elif year % 400:
  10.             return 0
  11.         else:
  12.             return 1


  13. res, sundays = 1, 0
  14. dates, leap_years = [], []
  15. for i in range(1901, 2001):
  16.     is_leap = leap_year(i)
  17.     month_day = [3, 0 + (1 * is_leap), 3, 2, 3, 2, 3, 3, 2, 3, 2, 3]
  18.     count_month = 1
  19.     for month in month_day:
  20.         count_month += 1
  21.         res += month
  22.         if not ((res + 1) % 7):
  23.             sundays += 1
  24.             if count_month < 13:
  25.                 date = "%s.%s" % (i, count_month)
  26.                 dates.append(date)
  27.             else:
  28.                 date = "%s.%s" % (i + 1, 1)
  29.                 dates.append(date)

  30. for i in dates:
  31.     print(i)
  32. print(sundays)
  33. print("It costs %f s" % (t.perf_counter() - start))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-10 09:54:45 | 显示全部楼层
  1. use chrono::prelude::*;

  2. pub fn run() -> Result<(), Box<dyn std::error::Error>> {
  3.     let mut count = 0;
  4.     let mut day = Utc.ymd(1901, 1, 1);
  5.     while day <= Utc.ymd(2000, 12, 31) {
  6.         if day.weekday() == Weekday::Sun && day.day() == 1 {count += 1;}
  7.         day += chrono::Duration::days(1)
  8.     }
  9.     println!("{count}");
  10.     Ok(())
  11. }
复制代码

[Project Euler 19]
171
[Task finished in 0ms]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-28 02:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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