0
0

More than 5 years have passed since last update.

# 円周上のCrossing(2013.9.16の過去問)

Posted at

``````module Doukaku.CrossCircle (solve) where

solve :: String -> String
solve = show . (`div` 2) . sum . map (uncurry countPoints) . searchLines

countPoints :: Eq a => [a] -> [a] -> Int
countPoints [] _ = 0
countPoints (x:xs) ys = (+ countPoints xs ys) . length . filter (== x) \$ ys

searchLines :: Eq a => [a] -> [([a], [a])]
searchLines = searchLines' []

searchLines' :: Eq a => [a] -> [a] -> [([a], [a])]
searchLines' _ [] = []
searchLines' ds (x:xs) = (searchLines'' [] x xs ds) ++ searchLines' (x:ds) xs

searchLines'' :: Eq a => [a] -> a -> [a] -> [a] -> [([a], [a])]
searchLines'' _ _ [] _ = []
searchLines'' ds t0 (t:ts) cp
| t == t0   = (ds, ts ++ cp) : tails
| otherwise = tails
where
tails = searchLines'' (t:ds) t0 ts cp
``````

この方法だと直線ごとに交点を数えるので一つの交点につき2回カウントしています。なので、最後に`2`で割る必要があります。

http://qiita.com/Nabetani/items/66806c9dc14a96f2fd42 に他の方の回答もありますので、見ると参考になるでしょう。

0
0
0

Register as a new user and use Qiita more conveniently

1. You get articles that match your needs
2. You can efficiently read back useful information
3. You can use dark theme
What you can do with signing up
0
0