プログラミングのお題スレ Part22 (862レス)
上下前次1-新
抽出解除 レス栞
670(6): デフォルトの名無しさん [] 03/13(木)20:35 ID:QP/8WHEA(1)
お題:数列が入力される。元の数列に逆順にした数列を減算したときの値を出力せよ
In < 12345
OUt > -41976 (12345 - 54321)
671(2): デフォルトの名無しさん [] 03/13(木)21:13 ID:SRpNsp20(1)
>>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
672(1): デフォルトの名無しさん [sage] 03/14(金)02:10 ID:wjeVVi0w(1/2)
>>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()));
}
673(1): デフォルトの名無しさん [sage] 03/14(金)02:30 ID:wjeVVi0w(2/2)
>>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()));
}
677: 9 [sage] 03/16(日)00:12 ID:8GU62dKf(1)
>>670 Perl5
use bigint;
print $_ - reverse($_), "\n" for 12345, 4922235242952026704037113243122008064;
実行結果
$ perl 22_670_reverse_minus_biginit.pl
-41976
314233029528909399960910650696685770
678: 警備員[Lv.23] [] 03/16(日)17:25 ID:wlGuyFJ7(1)
>>670
Kotlin
文字列にしてひっくり返しているだけの何の捻りもないプログラム
https://paiza.io/projects/VBq2l9lhzmUxVo6xAMuAHg
679: デフォルトの名無しさん [] 03/16(日)22:59 ID:wtk+s/+W(1)
>>670
PowerShellでジェネリック化(もどき?)
途中式や結果が入力値の型で表せない場合は$nullを返す。
function f($n)
{
$T = $n.GetType()
$s = [string]$n
try {
$n - $T::Parse(-join $s[-1..-$s.Length]) -as $T
} catch {
$null
}
}
12345, [BigInt]::Pow(12, 34), [byte]12, [sbyte]12, [sbyte]123, -123 |% {
"[$($_.GetType())]$_ → $(f $_)"
}
-- 実行結果 --
[int]12345 → -41976
[bigint]4922235242952026704037113243122008064 → 314233029528909399960910650696685770
[byte]12 →
[sbyte]12 → -9
[sbyte]123 →
[int]-123 →
上下前次1-新書関写板覧索設栞歴
スレ情報 赤レス抽出 画像レス抽出 歴の未読スレ AAサムネイル
ぬこの手 ぬこTOP 0.053s