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

小球走过路程计算

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};   // 计算总路程时,从反弹高度算
        }

    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
喵_coding:项目太烂了外卖+点评啊 而且寒假实习差不多到时候了 hc没多少了 要实在想要找那只能投投大厂试试了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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