[Haskell] CodeWars|Bouncing Balls
来源:互联网 发布:安卓sd卡数据恢复软件 编辑:程序博客网 时间:2024/06/05 22:47
https://www.codewars.com/kata/5544c7a5cb454edb3c000047/haskell
题目
一个小孩在一个超高建筑的n楼打球,高度为
如果上述3个参数的条件都满足了,输出一个正整数表示他母亲会看到的球的次数,否则返回-1表示参数错误。
样例
h = 3, bounce = 0.66, window = 1.5, result is 3h = 3, bounce = 1, window = 1.5, result is -1 (bounce >= 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
- 数学法
相当于求最大的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
阅读全文
0 0
- [Haskell] CodeWars|Bouncing Balls
- [Haskell] CodeWars|Printer Errors
- [Haskell] CodeWars|Vowel Count
- [Haskell] CodeWars|Your order, please
- [Haskell] CodeWars|Who likes it?
- [Haskell] CodeWars|Sum of Digits
- [Haskell] CodeWars|Integers: Recreation One
- Intro to Box2D with Cocos2D Tutorial: Bouncing Balls
- [Haskell] CodeWars|Growth of a Population
- [Haskell] CodeWars|Find the middle element
- [Haskell] CodeWars|Sum of odd numbers
- [Haskell] CodeWars|Build a pile of Cubes
- Android ApiDemo学习(五)Animation—— 1 Bouncing Balls
- Haskell
- Haskell
- A simple bouncing ball
- daily CodeWars
- Part2.CodeWars
- React update插件中 splice指令的理解
- Android (争取做到)最全的底部导航栏实现方法
- 【微信】Java 微信扫码支付(模式二)
- 倒序数
- PAT 甲级 1078. Hashing (25)
- [Haskell] CodeWars|Bouncing Balls
- HYSBZ
- 常用的Oracle的doc命令
- JAVA面试题整理之—基础篇第一部
- poj 2287 田忌赛马
- 剑指Offer------字符串的排列(全排列)
- ACM ICPC 2017 Warmup Contest 1 (NCPC 2016)
- 在IntelliJ IDEA下搭建基于Maven的spring boot框架
- 215[Medium]:Kth Largest Element in an Array