source: https://www.hackerrank.com/challenges/30-binary-numbers
module Main where
countMine :: Int -> Int -> Int
countMine c n | n == 0 = c
| otherwise = let tup = count1 0 n
in if snd tup == n then countMine c (div n 2)
else countMine (max c (fst tup)) (snd tup)
where count1 c n | mod n 2 == 0 = (c, n)
| otherwise = count1 (c+1) (div n 2)
main :: IO()
main = do
n <- fmap read getLine :: IO Int
print $ countMine 0 n
return ()
本文介绍了一个使用Haskell解决的问题:计算一个整数转换为二进制后的连续1的最大数量。通过递归算法,该解决方案展示了如何在Haskell中进行位操作和状态跟踪。
375

被折叠的 条评论
为什么被折叠?



