Problem 105
コンテンツ
http://projecteuler.net/index.php?section=problems&id=105
ちょっと考えたら、シンプルな解法に気がついた。
import Data.List monotone xs = all (f.sort$xs) [1..length xs `div` 2] where f ys n = (sum.take (n+1)) ys > (sum.take n.reverse) ys subsetSumNeq xs = all (f xs) [1..length xs `div` 2] where f ys n = all (null.tail).group.sort.map sum. comb ys $ n specialSum xs = monotone xs && subsetSumNeq xs main = do f <- readFile "sets.txt" let sets = map (read.("["++).(++"]")).lines $ f print.sum.map sum.filter specialSum $ sets comb _ = [[]] comb [] _ = [] comb (x:xs) (n+1) = map (x:) (comb xs n) ++ comb xs (n+1)
作成者 Toru Mano
最終更新時刻 2023-01-01 (c70d5a1)