牛客網彈地小球

一個小球,從高爲H的地方下落,下落彈地之後彈起高度爲下落時的一半,比如第一次彈起高度爲H/2,如此往復,計算從小球H 高度下落到第n 次彈地往返的總路程。

https://github.com/licunzhi/niuke/blob/master/src/com/sakura/aaaal/Main.java

package com.sakura.aaaal;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName Main
 * @Description 一個小球,從高爲H的地方下落,下落彈地之後彈起高度爲下落時的一半,
 * 比如第一次彈起高度爲H/2,如此往復,計算從小球H 高度下落到第n 次彈地往返的總路程。
 * @Author lcz
 * @Date 2019/03/21 15:19
 */
public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
        int count = Integer.parseInt(buf.readLine());
        List<Double> list = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            String[] strings = buf.readLine().split(" ");
            int height = Integer.parseInt(strings[0]);
            int num = Integer.parseInt(strings[1]);

            double length = 0.0;
            double temp = height;
            for (int j = 0; j < num; j++) {
                if (j == 0) {
                    length += temp;
                } else {
                    length += temp;
                    temp = temp / 2.0;
                }
            }
            list.add(length);
        }

        for (Double s : list) {
            System.out.printf("%.2f\n", s);
        }

    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章