プログラミングのお題スレ Part22 (854レス)
前次1-
抽出解除 必死チェッカー(本家) (べ) 自ID レス栞 あぼーん

リロード規制です。10分ほどで解除するので、他のブラウザへ避難してください。
672
(1): デフォルトの名無しさん [sage] 2025/03/14(金) 02:10:51.25 ID:wjeVVi0w(1/2) AAS
>>671
671(2): デフォルトの名無しさん [] 2025/03/13(木) 21:13:53.77 ID:SRpNsp20(1) AAS
>>670
PowerShell

function f([BigInt]$n)
{
  $c = [char[]][string]$n
  $n - [BigInt]-join $c[-1..-$c.length]
}

12345, [BigInt]::Pow(12, 34) |% {"$_ → $(f $_)"}

[実行結果]
12345 → -41976
4922235242952026704037113243122008064 → 314233029528909439273950854852378624
12の34乗は合っているけどその後の差がおかしくない?
4922235242952026704037113243122008064 から
4608002213423117304076202592425322294 を引いて
314233029528909399960910650696685770 が正解のところ
314233029528909439273950854852378624 となっているよ
正解は1の位が「4 - 4 = 0」になるはずだよね

>>670
670(6): デフォルトの名無しさん [] 2025/03/13(木) 20:35:03.45 ID:QP/8WHEA(1) AAS
お題:数列が入力される。元の数列に逆順にした数列を減算したときの値を出力せよ

In < 12345
OUt > -41976 (12345 - 54321)
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()));
}
673
(1): デフォルトの名無しさん [sage] 2025/03/14(金) 02:30:00.58 ID:wjeVVi0w(2/2) AAS
>>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()));
}
前次1-
スレ情報 赤レス抽出 画像レス抽出 歴の未読スレ AAサムネイル

ぬこの手 ぬこTOP 0.038s