結果

問題 No.39 桁の数字を入れ替え
ユーザー はむ吉🐹はむ吉🐹
提出日時 2015-12-30 17:28:13
言語 Haskell
(9.8.2)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 708 bytes
コンパイル時間 1,846 ms
コンパイル使用メモリ 170,880 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-10 05:16:32
合計ジャッジ時間 2,671 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 1 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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

ソースコード

diff #

-- 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
0