codewars——Bouncing Balls

A child is playing with a ball on the nth floor of a tall building. The height of this floor, h, is known.He drops the ball out of the window. The ball bounces (for example), to two-thirds of its height (a bounce of 0.66).His mother looks out of a window 1.5 meters from the ground.How many times will the mother see the ball pass in front of her window (including when it’s falling and bouncing?Three conditions must be met for a valid experiment:Float parameter “h” in meters must be greater than 0Float parameter “bounce” must be greater than 0 and less than 1Float parameter “window” must be less than h.If all three conditions above are fulfilled, return a positive integer, otherwise return -1.Note:The ball can only be seen if the height of the rebounding ball is stricty greater than the window parameter.

/*
一个孩子在一栋高楼的第N层玩球。这层楼的高度h是已知的。
他把球从窗口扔了出去。球反弹(例如)到其高度的三分之二(反弹0.66)。
他母亲从离地1.5米的窗户向外看。
妈妈会看到球在她窗前经过多少次(包括什么时候落下和弹跳)?
有效实验必须满足三个条件:
以米为单位的浮点参数“h”必须大于0
浮动参数“bounce”必须大于0且小于1
浮动参数“window”必须小于h。
如果满足上述三个条件,则返回正整数,否则返回-1。
注:
只有篮板球的高度比窗口参数严格时,才能看到球。
*/

#include <iostream>
using namespace std;
class Bouncingball
{
public:
    static int bouncingBall(double h, double bounce, double window);
};
int Bouncingball::bouncingBall(double h, double bounce, double window)
{
    int count = 0;
    if (h <= 0 || bounce >= 1 || bounce <= 0 || window >= h)
    {
        return -1;
    }
    if (h >= window)
        count += 1;
    while (true)
    {
        h *= bounce;
        if (h > window)
            count += 2;
        else
            break;
    }
    return count;
}

int main()
{
    Bouncingball ball;
    cout << ball.bouncingBall(30, 0.66, 1.5);
    return 0;
}

总结

用了循环,一开始一直想用递归方法,陷入怪圈

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值