- public class Ball
- {
- public static void main(String[] args)
- {
- Ball b=new Ball();
-
- int sum=b.fall(100,10);//10代表第10次落地;100是初始高度;
- double height=b.pop(10);//代表第n次弹跳后的高度;
-
- System.out.println(sum);//sum表示小球总共的运行距离
- System.out.println(height);
- }
-
- public int fall(int height,int count)
- {
- int sum=0;
- int h=height;
- for(int i=0;i<count;i++)
- {
- sum+=h;
- h/=2;
- }
- return (sum-height)*2+height;
- }
-
- public double pop(int count)
- {
- double height=0;
- for(int i=1;i<=count;i++)
- {
- height=(100/Math.pow(2, i));
- }
- return height;
- }
- }
- /*
- 打印结果:
- 294
- 0.09765625
- */
复制代码
|