プログラミングのお題スレ Part22 (857レス)
プログラミングのお題スレ Part22 http://mevius.5ch.net/test/read.cgi/tech/1691038333/
上
下
前次
1-
新
通常表示
512バイト分割
レス栞
抽出解除
必死チェッカー(本家)
(べ)
自ID
レス栞
あぼーん
リロード規制
です。10分ほどで解除するので、
他のブラウザ
へ避難してください。
60: デフォルトの名無しさん [sage] 2023/08/21(月) 12:55:28.09 ID:tqXtwiP4 // Rust 双方向イテレータ版 struct SameCalendarYear(u32); impl Iterator for SameCalendarYear { type Item = u32; fn next(&mut self) -> Option<u32> { common_next(self.0, false).map(|year| { self.0 = year; year }) } } impl DoubleEndedIterator for SameCalendarYear { fn next_back(&mut self) -> Option<u32> { common_next(self.0, true).map(|year| { self.0 = year; year }) } } fn common_next(start_year: u32, is_before: bool) -> Option<u32> { let mut day = 0; let mut year = start_year; while year > 0 { if is_before { year -= 1; } day += 1 + leap(year); if day >= 7 { day -= 7; } if !is_before { year += 1; } if day == 0 && leap(year) == leap(start_year) { return Some(year); } } None } fn leap(year: u32) -> u32 { (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) as u32 } http://mevius.5ch.net/test/read.cgi/tech/1691038333/60
61: デフォルトの名無しさん [sage] 2023/08/21(月) 12:57:26.59 ID:tqXtwiP4 >>59 指定した年と同じカレンダーの年を前後 n 個表示 (長くなるので今回は10個だけ表示) fn main() { let n = 10; for year in [2000, 2023, 2024, 2025, 2100] { println!("{year}年より前: {:?}", SameCalendarYear(year).rev().take(n).collect::<Vec<_>>()); println!("{year}年より後: {:?}", SameCalendarYear(year).take(n).collect::<Vec<_>>()); } } このRust公式の実行環境で実行やコード編集ができます https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=5e5a518120f75351ca5ff5fdd0bf4d8c // 実行結果 2000年より前: [1972, 1944, 1916, 1876, 1848, 1820, 1780, 1752, 1724, 1684] 2000年より後: [2028, 2056, 2084, 2124, 2152, 2180, 2220, 2248, 2276, 2316] 2023年より前: [2017, 2006, 1995, 1989, 1978, 1967, 1961, 1950, 1939, 1933] 2023年より後: [2034, 2045, 2051, 2062, 2073, 2079, 2090, 2102, 2113, 2119] 2024年より前: [1996, 1968, 1940, 1912, 1872, 1844, 1816, 1776, 1748, 1720] 2024年より後: [2052, 2080, 2120, 2148, 2176, 2216, 2244, 2272, 2312, 2340] 2025年より前: [2014, 2003, 1997, 1986, 1975, 1969, 1958, 1947, 1941, 1930] 2025年より後: [2031, 2042, 2053, 2059, 2070, 2081, 2087, 2098, 2110, 2121] 2100年より前: [2094, 2083, 2077, 2066, 2055, 2049, 2038, 2027, 2021, 2010] 2100年より後: [2106, 2117, 2123, 2134, 2145, 2151, 2162, 2173, 2179, 2190] http://mevius.5ch.net/test/read.cgi/tech/1691038333/61
メモ帳
(0/65535文字)
上
下
前次
1-
新
書
関
写
板
覧
索
設
栞
歴
スレ情報
赤レス抽出
画像レス抽出
歴の未読スレ
AAサムネイル
Google検索
Wikipedia
ぬこの手
ぬこTOP
0.046s