結果
問題 | No.39 桁の数字を入れ替え |
ユーザー |
![]() |
提出日時 | 2015-12-30 17:28:13 |
言語 | Haskell (9.10.1) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 708 bytes |
コンパイル時間 | 4,541 ms |
コンパイル使用メモリ | 173,312 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-10-02 06:48:57 |
合計ジャッジ時間 | 2,764 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 19 |
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.8.2/environments/default [1 of 2] Compiling Main ( Main.hs, Main.o ) [2 of 2] Linking a.out
ソースコード
-- Based on http://stackoverflow.com/questions/30551033/swap-two-elements-in-a-list-by-its-indices swapElementsAt :: Int -> Int -> [a] -> [a] swapElementsAt i j xs = left ++ [elemJ] ++ middle ++ [elemI] ++ right where elemI = (!!) xs $ min i j elemJ = (!!) xs $ max i j left = take i xs middle = take (j - i - 1) $ flip drop xs $ i + 1 right = flip drop xs $ j + 1 swapDigitsAt :: Int -> Int -> Int -> Int swapDigitsAt i j = read . swapElementsAt i j . show getMaximum :: Int -> Int getMaximum n = maximum $ n : [swapDigitsAt i j n | j <- [1 .. l - 1], i <- [0 .. j - 1]] where l = length $ show n main :: IO () main = print . getMaximum =<< readLn