結果

問題 No.39 桁の数字を入れ替え
ユーザー taktak
提出日時 2019-07-04 14:52:11
言語 F#
(F# 4.0)
結果
WA  
実行時間 -
コード長 1,441 bytes
コンパイル時間 11,190 ms
コンパイル使用メモリ 192,984 KB
実行使用メモリ 31,392 KB
最終ジャッジ日時 2023-10-19 07:36:17
合計ジャッジ時間 13,879 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
31,392 KB
testcase_01 AC 72 ms
31,392 KB
testcase_02 AC 69 ms
31,392 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 72 ms
31,392 KB
testcase_08 AC 70 ms
31,392 KB
testcase_09 AC 71 ms
31,392 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 71 ms
31,392 KB
testcase_15 AC 71 ms
31,392 KB
testcase_16 AC 71 ms
31,392 KB
testcase_17 AC 70 ms
31,392 KB
testcase_18 AC 70 ms
31,392 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.fsproj を復元しました (633 ms)。
MSBuild のバージョン 17.7.3+8ec440e68 (.NET)
  main -> /home/judge/data/code/bin/Release/net7.0/main.dll
  main -> /home/judge/data/code/bin/Release/net7.0/publish/

ソースコード

diff #

module Yuki

open System

let inline ret x = Some x
let inline (>>=) (m: Option<'a>) f =
    match m with
    | Some x -> f x
    | None -> None

type MaybeBuilder () =
    member __.Return(x) = ret x
    member __.Bind(m, f) = m >>= f

let maybe = MaybeBuilder ()

let solve n =
    let num2list n =
        let rec f acc n =
            if n < 10 then n :: acc
            else f (n % 10 :: acc) (n / 10)
        f [] n
    let list2num xs =
        let rec f acc =
            function
            | [] -> acc
            | x :: xs -> f (acc * 10 + x) xs
        f 0 xs
    let swap idx1 idx2 xs =
        xs
        |> List.mapi (fun i v ->
            match i with
            | _ when i = idx1 -> xs.[idx2]
            | _ when i = idx2 -> xs.[idx1]
            | _ -> v)        

    let digits = num2list n
    let indexedDigits = List.indexed digits
    let maxDigit = List.max digits
    maybe {
        let! swapLeftIdx =
            indexedDigits
            |> List.tryFind (fun (_, x) -> x <= maxDigit)
            |> function
            | Some (idx, _) -> Some idx
            | None -> None
        let swapRightIdx =
            indexedDigits
            |> List.findBack (fun (_, x) -> x = maxDigit)
            |> fun (idx, _) -> idx
        return digits |> swap swapLeftIdx swapRightIdx |> list2num
    }
    |> function
    | Some x -> x
    | None -> n

let N = int <| Console.ReadLine()

solve N
|> Console.WriteLine
0