我正在对Project Euler执行problem 112,并提出了以下内容来测试示例用例(我将answer中的数字更改为0.99以获得真正的答案):
isIncre x | x == 99 = False
| otherwise = isIncre' x
where
isIncre' x = ???
isDecre x = isIncre (read $ reverse $ show x :: Int)
isBouncy x = (isIncre x == False) && (isDecre x == False)
bouncers x = length [n|n<-[1..x],isBouncy n]
nonBouncers x = length [n|n<-[1..x],(isBouncy n) == False]
answer = head [x|x<-[1..],((bouncers x) / (nonBouncers x)) == 0.5]但我不知道怎么做的是定义一个函数isIncre',它测试一个数字中的数字是否大于或等于它们左边的数字。我知道它需要递归地完成,但是怎样做呢?
顺便说一句,我知道我只能对两个浮点数使用/,但是我如何才能使bouncers的输出成为浮点数而不是整数呢?
编辑:
谢谢你的帮助,但当我将isIncre更改为时,它不喜欢=:
isIncre x | x <= 99 = False
| otherwise = isIncre' (mshow x)
where
isIncre' (x:y:xs) = (x <= y) && (isIncre' (y:xs))
isIncre' _ = True发布于 2009-11-24 01:30:04
如果你有intetger的字符串表示,我会使用非常好的isIncre函数版本。
isIncre :: (Ord a) => [a] -> Bool
isIncre list = and $ zipWith (<=) list (tail list)如果不是,就用show编写它。
isIncreNum :: Integer -> Bool
isIncreNum = isIncre . showhttps://stackoverflow.com/questions/1783668
复制相似问题