B1tetheDust 发表于 2022-6-8 16:02:43

番杰 发表于 2021-10-29 13:57


import time as t

start = t.perf_counter()


def leap_year(year):
    if year % 4:
      return 0
    else:
      if year % 100:
            return 1
      elif year % 400:
            return 0
      else:
            return 1


res, sundays = 1, 0
dates, leap_years = [], []
for i in range(1901, 2001):
    is_leap = leap_year(i)
    month_day =
    count_month = 1
    for month in month_day:
      count_month += 1
      res += month
      if not ((res + 1) % 7):
            sundays += 1
            if count_month < 13:
                date = "%s.%s" % (i, count_month)
                dates.append(date)
            else:
                date = "%s.%s" % (i + 1, 1)
                dates.append(date)

for i in dates:
    print(i)
print(sundays)
print("It costs %f s" % (t.perf_counter() - start))

B1tetheDust 发表于 2022-6-8 16:03:46

import time as t

start = t.perf_counter()


def leap_year(year):
    if year % 4:
      return 0
    else:
      if year % 100:
            return 1
      elif year % 400:
            return 0
      else:
            return 1


res, sundays = 1, 0
dates, leap_years = [], []
for i in range(1901, 2001):
    is_leap = leap_year(i)
    month_day =
    count_month = 1
    for month in month_day:
      count_month += 1
      res += month
      if not ((res + 1) % 7):
            sundays += 1
            if count_month < 13:
                date = "%s.%s" % (i, count_month)
                dates.append(date)
            else:
                date = "%s.%s" % (i + 1, 1)
                dates.append(date)

for i in dates:
    print(i)
print(sundays)
print("It costs %f s" % (t.perf_counter() - start))

Asss-whom 发表于 2022-8-10 09:54:45

use chrono::prelude::*;

pub fn run() -> Result<(), Box<dyn std::error::Error>> {
    let mut count = 0;
    let mut day = Utc.ymd(1901, 1, 1);
    while day <= Utc.ymd(2000, 12, 31) {
      if day.weekday() == Weekday::Sun && day.day() == 1 {count += 1;}
      day += chrono::Duration::days(1)
    }
    println!("{count}");
    Ok(())
}

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