[Haskell] CodeWars|Bouncing Balls

来源:互联网 发布:安卓sd卡数据恢复软件 编辑:程序博客网 时间:2024/06/05 22:47

https://www.codewars.com/kata/5544c7a5cb454edb3c000047/haskell

题目

一个小孩在一个超高建筑的n楼打球,高度为h(h>0),他把球丢出了建筑,垂直下落并弹起,每次弹起的高度是上一次的bounce(0<bounce<1)倍。他母亲从window(window<h)高度往窗外看。他母亲会看到多少次球呢?(看到球定义为球经过window的高度)
如果上述3个参数的条件都满足了,输出一个正整数表示他母亲会看到的球的次数,否则返回-1表示参数错误。

样例

h = 3, bounce = 0.66, window = 1.5, result is 3h = 3, bounce = 1, window = 1.5, result is -1 (bounce >= 1).

题解

  1. 模拟法
module Codewars.Kata.BouncingBall wherebouncingBall :: Double -> Double -> Double -> IntegerbouncingBall' h bounce window    | h < window = 0    | h == window = 1    | otherwise = 2 + bouncingBall' (h * bounce) bounce windowbouncingBall h bounce window    | bounce >= 1 || bounce <= 0 || h <= 0 || window >= h = -1    | otherwise = 1 + bouncingBall' (h * bounce) bounce window
  1. 数学法
    相当于求最大的n,使得
    h×(bounce)n>window

    n<logbouncewindowh

    那么有
    n=logbouncewindowh

    n表示弹跳的次数,显然母亲看到的次数就是2n+1了。
bouncingBall h b w  | not (h > 0 && 0 < b && b < 1 && w < h) = -1  | otherwise = floor (logBase b $ w / h) * 2 + 1