結果

問題 No.499 7進数変換
ユーザー くれちー
提出日時 2017-04-09 11:18:07
言語 Haskell
(9.10.1)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,022 bytes
コンパイル時間 7,770 ms
コンパイル使用メモリ 172,672 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-18 01:25:43
合計ジャッジ時間 8,670 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 31
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 #

import Data.Char (chr, ord)

intToChar :: Int -> Char
intToChar x
    |  0 <= x && x <=  9 = chr $ ord '0' + x
    | 10 <= x && x <= 35 = chr $ ord 'A' - 10 + x
    | otherwise          = error "out of range"
    
charToInt :: Char -> Int
charToInt c
    | '0' <= c && c <= '9' = ord c - ord '0'
    | 'A' <= c && c <= 'Z' = ord c - ord 'A' + 10
    | otherwise            = error "out of range"

toDecimal :: Integral a => Int -> String -> a
toDecimal fr fx = sum $ zipWith (\a b -> a * fr' ^ b) fx' $ reverse [0..length fx' - 1]
    where
        fx' = dropWhile (== 0) $ map (fromIntegral . charToInt) fx
        fr' = fromIntegral fr

fromDecimal' :: Integral a => a -> Int -> [a]
fromDecimal' fx tr
    | fx < tr'  = fx : []
    | otherwise = fx `mod` tr' : fromDecimal' (fx `div` tr') tr
    where
        tr' = fromIntegral tr

fromDecimal :: Integral a => a -> Int -> String
fromDecimal fx tr = map (intToChar . fromIntegral) $ reverse $ fromDecimal' fx tr

main = do
    n <- readLn
    putStrLn $ fromDecimal n 7
0