描述

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 0
Float parameter “bounce” must be greater than 0 and less than 1
Float 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 strictly greater than the window parameter.

分析

方法1:递归实现。每次球弹起到最高点,之后的过程可以看做一个起始高度减少的流程相同的问题。终止条件为起始高度低于窗户高度。
方法2:递推实现。

实现

方法1:

func BouncingBall(h, bounce, window float64) int {if h <= 0 || bounce <= 0 || bounce >= 1 || window >= h { return -1 }return 2 + BouncingBall((h * bounce), bounce, window)
}

方法2:

func BouncingBall(h, bounce, window float64) int {if h <= 0 || bounce <= 0 || bounce >= 1 || window >= h { return -1 }sum := 0for ; h * bounce > window; h *= bounce { sum += 2 }return sum + 1
}

Go语言实现Bouncing Balls相关推荐

  1. Go语言基础(codewars---6kyu和5kyu)

    文章目录 说明 一.String相关题 1.Stop gninnipS My sdroW!(6kyu) 2.Consecutive strings(6kyu) 3.Build Tower(6kyu) ...

  2. Go并发编程中的那些事[译]

    原文地址:Concurrent programming 原文作者:StefanNilsson 译文出自:掘金翻译计划 本文永久链接:github.com/xitu/gold-m- 译者:kobehah ...

  3. 一个人开发手游有多难

    [编者按]本文作者朱念洋. 谨以此文献给与我同样有创业梦想的程序员们. 我相信像我一样的程序员一定大有人在,一个人.一身技术.一腔热血,想要涉足手游领域,却不清楚会投入多少. 所以,我把自己的这次游戏 ...

  4. 达拉草201771010105《面向对象程序设计(java)》第十六周学习总结

    达拉草201771010105<面向对象程序设计(java)>第十六周学习总结 第一部分:理论知识 1.程序与进程的概念: (1)程序是一段静态的代码,它是应用程序执行的蓝 本. (2)进 ...

  5. 201771010126 王燕《面向对象程序设计(Java)》第十六周学习总结

    实验十六  线程技术 实验时间 2017-12-8 1.实验目的与要求 (1) 掌握线程概念: ‐多线程 是进程执行过中产生的多条线索. 是进程执行过中产生的多条线索. 是进程执行过中产生的多条线索. ...

  6. 201771010109焦旭超《面向对象程序设计(java)》第十六周学习总结

    1.实验目的与要求 (1) 掌握线程概念: (2) 掌握线程创建的两种技术: (3) 理解和掌握线程的优先级属性及调度方法: (4) 掌握线程同步的概念及实现技术: 2.实验内容和步骤 实验1:测试程 ...

  7. WPF特效-粒子动画

    原文:WPF特效-粒子动画 WPF实现泡泡龙小游戏效果. /// -Ball to Ball Collision - Detection and Handling     /// http://sta ...

  8. 杨玲 201771010133《面向对象程序设计(java)》第十六周学习总结

    <面向对象程序设计(java)>第十六周学习总结 第一部分:理论知识学习部分 1.程序是一段静态的代码,它是应用程序执行的蓝本.进程是程序的一次动态执行,它对应了从代码加载.执行至执行完毕 ...

  9. Android Animation时间插入器Interpolator

    http://blog.csdn.net/lixiang0522/article/details/7893239 属性动画系统是非常强健的框架,允许你移动几乎任何东西.你可以定义一个动画去改变任何对象 ...

  10. html2canvas教程_HTML5 Canvas教程:简介

    html2canvas教程 Interested in CSS animation? Check out Creating Animations with CSS, a complete course ...

最新文章

  1. ContentProvider访问问题
  2. BFS:图的最短路径  Aizu - 0558 ​​​​​​​Cheese
  3. R语言编程 第一讲 变量与赋值
  4. 数论--康托展开与逆康托展开模板
  5. maven配置sqlServer的依赖
  6. 分布式Session的几种实现方式
  7. mysql硬盘备份_原创-在mysql中把里面的数据库备份到自己的硬盘上
  8. python函数调用的三种方式_python函数调用的四种方式
  9. Javascript第五章删除、克隆、插入、替换方法源码第九课
  10. 9个笑话 顿悟9个人生道理
  11. 基于微信小程序的电影院购票系统丨毕业设计源码
  12. 设计模式——终结者模式
  13. 练习-Java类和对象之包的定义
  14. 微信群发工具,纯Python编写~
  15. 如何管理NVivo的查询结果
  16. 微信小程序 展示地图指定位置导航
  17. Typora高亮颜色设置
  18. 宣化科技职业学院计算机哪个校区,宣化科技职业学院宿舍怎么样
  19. Numpy基本用法:ndarray的数据类型
  20. mysql 1025_ERROR 1025 (HY000): Error on rename of to

热门文章

  1. 愿天下有情人都是失散多年的兄妹(dfs)
  2. 思科交换机密码恢复方法介绍
  3. WordPress Cookies因预料之外的输出被阻止
  4. 使用mmap遇到总线错误bus error
  5. Layui Table 的列隐藏问题
  6. 记一次小米的Java面试
  7. echarts:实现3D柱状图 柱状图渐变颜色设置
  8. PageHelper 分页不起作用的坑
  9. 3星|《三联生活周刊》2017年7-8期:吃货逛西班牙、土耳其、美国加州、泰国、中国台湾...
  10. 女神教你字符串——AC不只是一个梦想