问题:一球从某高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第n次落地时,共经过多少米?第n次反弹多高?...

本文通过Java程序解决了一球从特定高度自由落下并反弹的问题,计算了球在第10次落地时所经过的总距离及反弹高度。采用递归算法进行计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.util.Scanner;

//题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?
public class BallTest {
    public static void main(String[] args) {
        double firsthight;
        int n;
        Scanner in = new Scanner (System.in);//从键盘读取两个数据
        System.out.println("请输入从多少米落下:");
        firsthight = in.nextDouble();
        System.out.println("请输入第几次落下:");
        n = in.nextInt();
    System.out.println("从"+firsthight+"m"+"第"+n+"次落地经过的距离"+countTotalLong(n,firsthight)+"m");
    System.out.println("从"+firsthight+"m"+"第"+n+"次反弹高度"+countHeight(n,firsthight)+"m");
    }
    public static double countTotalLong(int n,double firsthight){//计算总的路径
        double totalLong =firsthight;
        for(int i = 0;i<n-1;i++){
            totalLong += firsthight/(double)(Math.pow(2,i));
        }
        return totalLong;
    }
    public static double countHeight(int n,double firsthight){ //计算每次反弹高度
        return firsthight/(double)(Math.pow(2,n));
    }
}

 

转载于:https://www.cnblogs.com/iamhenanese/p/5466924.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值