码迷,mamicode.com
首页 > 其他好文 > 详细

【华为OJ】【031-求小球落地5次后所经历的路程和第5次反弹的高度】

时间:2016-05-09 08:36:59      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

【华为OJ】【算法总篇章】


【华为OJ】【031-求小球落地5次后所经历的路程和第5次反弹的高度】

【工程下载】


题目描述

假设一个球从任意高度自由落下,每次落地后反跳回原高度的一半; 再落下, 求它在第5次落地时,共经历多少米?第5次反弹多高? 

/**
 * 统计出第5次落地时,共经过多少米?
 * 
 * @param high 球的起始高度
 * @return 英文字母的个数
 */
public static double getJourney(int high) {
    return 0;
}

/**
 * 统计出第5次反弹多高?
 * 
 * @param high 球的起始高度
 * @return 空格的个数
 */
public static double getTenthHigh(int high) {
    return 0;
}

输入描述

输入起始高度,int型

输出描述

分别输出第5次落地时,共经过多少米第5次反弹多高

输入例子

1

输出例子

2.875
0.03125

算法实现

import java.util.Scanner;

/**
 * Author: 王俊超
 * Date: 2015-12-24 14:13
 * All Rights Reserved !!!
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
//        Scanner scanner = new Scanner(Main.class.getClassLoader().getResourceAsStream("data.txt"));
        while (scanner.hasNext()) {
            double h = scanner.nextDouble();
            System.out.printf("%g\n", getJourney(h));
            System.out.printf("%g\n", getTenthHigh(h));
        }

        scanner.close();
    }

    private static double getTenthHigh(double h) {
        return h / 32;
    }

    private static double getJourney(double h) {

        double up = (Math.pow(0.5, 4) - 1) / (0.5 - 1);
        double down = (Math.pow(0.5, 5) - 1) / (0.5 - 1);

        return h * 0.5 * up + h * down;
    }
}

【华为OJ】【031-求小球落地5次后所经历的路程和第5次反弹的高度】

标签:

原文地址:http://blog.csdn.net/derrantcm/article/details/51350403

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!