题解 | #小球走过路程计算 - 递归解法#

小球走过路程计算

https://www.nowcoder.com/practice/ddbb7021c0a7452f9044564234616913

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        float h=scanner.nextFloat();
        int n =scanner.nextInt();

        //write your code here......
        double [] result = get_h( h, n);
        System.out.println(String.format("%.3f", result[0])+" "+String.format("%.3f", result[1]));
        //输出格式为:System.out.println(String.format("%.3f", h)+" "+String.format("%.3f", sum));
        

    }
    public static  double[] get_h( float h, int n){
        if (n == 1){
            return new double[]{h/2 , h}; 
        }else{
            double [] pre = get_h(h, n-1);
            return new double [] {pre[0]/2, pre[1] +pre[0]*2};   // 计算总路程时,从反弹高度算
        }

    }
}

全部评论

相关推荐

10-13 13:49
南京大学 财务
饿魔:笑死我了,你简直是个天才
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务