プログラミングのお題スレ Part22 (857レス)
プログラミングのお題スレ Part22 http://mevius.5ch.net/test/read.cgi/tech/1691038333/
上
下
前次
1-
新
通常表示
512バイト分割
レス栞
抽出解除
必死チェッカー(本家)
(べ)
自ID
レス栞
あぼーん
リロード規制
です。10分ほどで解除するので、
他のブラウザ
へ避難してください。
672: デフォルトの名無しさん [sage] 2025/03/14(金) 02:10:51.25 ID:wjeVVi0w >>671 12の34乗は合っているけどその後の差がおかしくない? 4922235242952026704037113243122008064 から 4608002213423117304076202592425322294 を引いて 314233029528909399960910650696685770 が正解のところ 314233029528909439273950854852378624 となっているよ 正解は1の位が「4 - 4 = 0」になるはずだよね >>670 Rust 逆文字列を生成する版 use num::BigInt; fn odai(input: &str) -> Option<String> { let rev_input: String = input.chars().rev().collect(); let x: BigInt = input.parse().ok()?; let y: BigInt = rev_input.parse().ok()?; Some((x - y).to_string()) } fn main() { assert_eq!(odai("12345"), Some("-41976".to_string())); assert_eq!(odai("4922235242952026704037113243122008064"), Some("314233029528909399960910650696685770".to_string())); } http://mevius.5ch.net/test/read.cgi/tech/1691038333/672
673: デフォルトの名無しさん [sage] 2025/03/14(金) 02:30:00.58 ID:wjeVVi0w >>670 Rust 逆文字列を生成しない&整数ジェネリック版 use num::{BigInt, CheckedAdd, CheckedMul, CheckedSub, FromPrimitive}; fn chars_to_integer<X>(input: impl Iterator<Item = char>) -> Option<X> where X: FromPrimitive + CheckedMul + CheckedAdd, { let (zero, ten) = (X::from_u32(0).unwrap(), X::from_u32(10).unwrap()); input .map(|c| X::from_u32(c.to_digit(10)?)) .try_fold(zero, |acc, x| acc.checked_mul(&ten)?.checked_add(&x?)) } fn odai<X>(input: &str) -> Option<X> where X: FromPrimitive + CheckedMul + CheckedAdd + CheckedSub, { let x = chars_to_integer::<X>(input.chars())?; let y = chars_to_integer::<X>(input.chars().rev())?; x.checked_sub(&y) } fn main() { assert_eq!(odai::<i64>("12345"), Some(-41976)); assert_eq!(odai::<BigInt>("4922235242952026704037113243122008064"), Some("314233029528909399960910650696685770".parse::<BigInt>().unwrap())); } http://mevius.5ch.net/test/read.cgi/tech/1691038333/673
メモ帳
(0/65535文字)
上
下
前次
1-
新
書
関
写
板
覧
索
設
栞
歴
スレ情報
赤レス抽出
画像レス抽出
歴の未読スレ
AAサムネイル
Google検索
Wikipedia
ぬこの手
ぬこTOP
0.034s