首页 > 试题广场 >

求小球落地5次后所经历的路程和第5次反弹的高度

[编程题]求小球落地5次后所经历的路程和第5次反弹的高度
  • 热度指数:156583 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

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


数据范围:输入的小球初始高度满足 ,且保证是一个整数


输入描述:

输入起始高度,int型



输出描述:
分别输出第5次落地时,共经过多少米以及第5次反弹多高。
注意:你可以认为你输出保留六位或以上小数的结果可以通过此题。
示例1

输入

1

输出

2.875
0.03125
求小球落地5次后所经历的路程和第5次反弹的高度
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int height = scanner.nextInt();
            float total = height;
            double[] arr = new double[5];
            arr[0] = height;
            for (int i = 1; i < 5; i++) {
                arr[i] = arr[i - 1] / 2;
                total += arr[i] * 2;
            }
            System.out.println(total);
            System.out.println(arr[4] / 2);
        }
    }
}

编辑于 2024-04-07 19:41:18 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        double h = in.nextDouble();
        double temp = h/2;
        for(int i=1;i<5;i++){
            h += temp*2;
            temp= temp/2;
        }
        System.out.println(h);
        System.out.println(temp);
    }
}

发表于 2024-03-09 20:03:36 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
              int high = in.nextInt();
            //16  16  8;8   4;4   2;2   1;1
            double fall = 0;
            double temp = (double) high;
            double sum = 0;
            for (int i = 1; i <= 5; i++) {
                fall = temp;
                temp /=2;
                if(i < 5){
                    sum += fall+temp;
                }else{
                    sum +=fall;
                }
            }
            System.out.println(sum);
            System.out.println(temp);
        }
    }
}

发表于 2024-01-05 09:42:54 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        float h = in.nextFloat();
        float s = 0;
        for(int i = 0; i < 5; i++){
            s +=  (i == 0 ? h : 2*h);
            h /= 2;
        }
        System.out.println(s);
        System.out.println(h);
    }
}

发表于 2023-11-24 00:35:33 回复(0)
看似复杂,实质再求等比数列的问题。
等比数列求和公式 (1) 等比数列:an=a1×q^(n-1); 推广式:an=am×q^(n-m); (2) 求和公式:Sn=n×a1 (q=1) Sn=a1(1-q^n)/(1-q) =(a1-an×q)/(1-q) (q≠1) (q为公比,n为项数)
假设高度为n
第一次落地高度:n
第二次:n+ (n/2) *2
第三次:n+ (n/2^2 )*2
............
第n次落地反弹:a*(1/2)^n
发表于 2023-09-20 18:03:32 回复(0)
public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            double distance5th = n*2.87500;
            double heights5th = n*0.031250;
            System.out.println(distance5th);
            System.out.println(heights5th);
        }
    }

发表于 2023-08-04 20:08:45 回复(0)
这题的反弹高度是固定的,所以只需要代入公式即可,而公式恰巧题目样例输入已经给了
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            // 总路程
            System.out.println(a * 2.875f);
            // 最后一次反弹高度
            System.out.println(a * 0.03125f);
        }
    }
}


发表于 2023-06-07 18:35:38 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        Double height=in.nextDouble();
        Double sum=0.0;
        for(int i=1;i<=5;i++){
            sum+=height+height/2;
            height/=2;
        }
        sum-=height;
        System.out.println(sum);
        System.out.print(height);
    }
}

发表于 2023-05-28 15:06:19 回复(0)
华为OD深圳/西安岗位,部门急招,薪资20-80w. 部门有专门机考辅导人员,每周开视频讲座。欢迎叨扰:***********
编辑于 2023-05-10 15:59:06 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        System.out.println(2.875 * n);
        System.out.println(0.03125 * n);
    }
}

发表于 2023-04-18 20:13:02 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int h=in.nextInt();
        System.out.println(2.875*h);
        System.out.println(0.03125*h);
    }
}
发表于 2023-04-08 10:51:25 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 获取小球的初始高度
        double height = in.nextDouble();
        // 定义一个变量,表示小球经过的路程
        double distance = 0;
        // 定义一个变量,表示小球反弹的高度
        double reboundHeight = 0;
        // 循环模拟反弹
        for (int i = 0; i < 5; i++) {
            if (i == 0) {
                // 首次反弹为小球初始高度一半
                reboundHeight = height / 2;
                // 首次距离为小球初始高度
                distance = height;
            } else {
                // 小球经过的距离等于初始高度+2倍反弹高度
                distance = distance + reboundHeight*2;
                // 每循环一次,反弹高度为原来的一半
                reboundHeight = reboundHeight / 2;
            }
        }
        // 输出结果
        System.out.println(distance);
        System.out.println(reboundHeight);

    }
}

发表于 2023-02-09 10:06:46 回复(0)
import java.util.Scanner;

public class Main {
 
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
         int h = in.nextInt();
         Double dp[] = new Double[5+1];
         Double s[] = new Double[5+1];
        dp[0] = h * 1.0d;
         for (int i = 1; i < dp.length; i++) {
            dp[i] = dp[i - 1] * 0.5;
            
            if(i ==1){
                s[i]=dp[i-1];
            }else{
                s[i] = s[i-1] + dp[i-1]*2;
            }
            
        }
         System.out.println(s[dp.length-1]);
         System.out.println(dp[dp.length-1]);
    }
   
}
发表于 2022-11-27 21:10:43 回复(0)
import java.io.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s;
        while ((s = br.readLine()) != null) {
            double H = Double.parseDouble(s);
            double S = 0; //记录总路程
            double h;
            for (int i = 1; i < 5; i++) {
                h = H / 2;
                S += H + h;
                H = h;
            }
            S += H;
            h = H / 2;
            System.out.println(S + "\n" + h);
        }
    }
}

发表于 2022-09-30 18:21:39 回复(0)
除2总是有精确结果的,直接println了,没管有效数字。
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        double height = Double.parseDouble(br.readLine());
        double distance = 0;
        for (int i = 0; i < 5; i++) {
            distance += height;
            height /= 2;
            if (i == 4) {
                System.out.println(distance);
                System.out.println(height);
            }
            distance += height;
        }
    }
}


发表于 2022-09-02 19:07:13 回复(0)
import java.io.BufferedReader;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);
        double num = scan.nextDouble();

        double sumHeight = 0;
        double lastHeight = num;

        for(int i=0;i<5;i++){
            lastHeight = lastHeight/2;   // 反弹后的高度
            sumHeight = sumHeight+3*lastHeight;   // 代表落下高度+反弹到一半的高度
        }
        System.out.println(sumHeight-lastHeight);   //第五次落地不在记录反弹的那部分高度,所以减去
        System.out.print(lastHeight);

    }
}
发表于 2022-08-31 14:08:34 回复(0)
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        int height = Integer.parseInt(str);
        double sOfFifth = 0; // 路径
        double hOfFifth = 0; // 第五次反弹的高度
        double h = height;
        for(int i = 0; i < 5; i++){
            sOfFifth += 2 * h;
            h = h / 2;
        }
        System.out.println(sOfFifth - height);
        System.out.println(h);
    }
}

发表于 2022-08-15 14:50:48 回复(0)

问题信息

难度:
59条回答 36855浏览

热门推荐

通过挑战的用户

查看代码