-
-
Save stripe-q/b8fadb38d246b10d77539f3c595e8eea to your computer and use it in GitHub Desktop.
소인수분해정보로 최대공약수찾기 (하스켈)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List | |
import Data.Function | |
type Fac = [(Int, Int)] | |
list2tuples :: [Int] -> Fac | |
list2tuples [] = [] | |
list2tuples (a:b:xs) = (a,b):(list2tuples xs) | |
list2tuples _ = error "Error" | |
tuples2list = concatMap (\(a, b) -> [a, b]) | |
myGCD :: Fac -> Fac -> Fac | |
myGCD xs ys = zip cs es | |
where | |
cs = (intersect `on` map fst) xs ys | |
findE c = snd . head . dropWhile ((/= c) . fst) | |
findMinE c = (min `on` findE c) xs ys | |
es = map findMinE cs | |
solve = do | |
getLine | |
inputData <- (map (map read . words) . lines) <$> getContents | |
let result = tuples2list . folr1 myGCD . map list2tuples $ inputData | |
putStrLn . unwords . map show $ result | |
main = solve |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment