首页 > 试题广场 >

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

[编程题]求小球落地5次后所经历的路程和第5次反弹的高度
  • 热度指数:167770 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}假设有一个小球从 h 米高度自由落下,我们不考虑真实的物理模型,而是简洁的假定,该小球每次落地后会反弹回原高度的一半;再落下,再反弹;……。
\hspace{15pt}求小球在第五次落地时所经历的路程和第五次反弹的高度。

\hspace{15pt}在本题中,路程的计算仅需考虑垂直方向的变化。

输入描述:
\hspace{15pt}在一行上输入一个整数 h \left(1 \leqq h \leqq 10^3\right) 代表小球的初始高度。


输出描述:
\hspace{15pt}第一行输出一个实数,代表小球在第五次落地时所经历的路程。
\hspace{15pt}第二行输出一个实数,代表第五次反弹的高度。

\hspace{15pt}由于实数的计算存在误差,当误差的量级不超过 10^{-6} 时,您的答案都将被接受。具体来说,设您的答案为 a ,标准答案为 b ,当且仅当 \frac{|a-b|}{\max(1,|b|)}\leqq 10^{-6} 时,您的答案将被接受。
示例1

输入

1

输出

2.875
0.03125

说明

\hspace{15pt}第一次反弹高度为 0.5 米,第二次反弹高度为 0.25 米,第三次反弹高度为 0.125 米,第四次反弹高度为 0.0625 米,第五次反弹高度为 0.03125 米。
\hspace{15pt}截止第五次落地,总路程为 1 + 2 \times \left(0.5 + 0.25 + 0.125 + 0.0625\right) = 2.875 米。
a = float(input())
b = 0
for i in range(5):
    b += a + a*0.5
    a = a*0.5
print(b-a)    # 最后一次落地不反弹,要减去a
print(a)

发表于 2022-07-12 22:08:51 回复(0)
大佬的等比例缩放有点狠。。。
import java.util.Scanner;

/**
 * @author aiker
 * @since 2021/11/6
 */
public class Main {

    private static double[] calculateTrail(double height, int times) {
        double[] result = new double[2];
        double trail;
        if (times == 1) {
            result[0] = height; result[1] = height / 2;
            return result;
        }

        trail = height;
        for (int i = 1; i < times; i++) {
            trail += height;
            height /= 2;
        }

        result[0] = trail; result[1] = height / 2;
        return result;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            double nextInt = scanner.nextDouble();
            double[] result = calculateTrail(nextInt, 5);
            System.out.println(result[0]);
            System.out.println(result[1]);
        }
    }
    
}


发表于 2021-11-06 15:49:09 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int high = in.nextInt();
        float fhigh = high;
        float sumhigh=0;
        for(int i=0;i<4;++i){
            sumhigh+=fhigh/2+fhigh;
            fhigh/=2;
        } 
        sumhigh+=fhigh;
        fhigh/=2;
        System.out.println(sumhigh+"\n"+fhigh);
    }
}

发表于 2021-09-30 09:53:36 回复(0)
答案不对就说答案不对,为啥说我格式错误啊🤣。查了半天格式,然后测样例时候发现h不大对(忘记最后除以一下2了),然后改了下就对了?????
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    double h;
    cin>>h;
    double ans=h; //第一次落地时的高度
    for(int i=0;i<4;++i) {
        ans+=h; //反弹的高度是原来的一半,但是起-落的过程经历的距离是反弹高度的两倍,仍是h
        h/=2; //第i+2次落地之前弹起的高度
    }
    h/=2;
    printf("%0.6f\n%0.6f\n",ans,h);
    return 0;
}
发表于 2021-06-26 21:10:20 回复(0)
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        System.out.printf("%.6f\n",n*2.875);
        System.out.printf("%.6f\n",0.03125*n);
    }
}
发表于 2021-05-22 10:15:46 回复(3)
while 1:
    try:
        num = int(input())
        print(num*2.875)
        print((num*0.03125))
    except:
        break

发表于 2020-12-29 17:45:18 回复(1)
都用double为啥我float型也过了
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int data;
    while(cin>>data)
    {
        float h,sum,flag1,flag2,flag3,flag4,flag5;
        h=data;
        flag1=h/2;
        flag2=flag1/2;
        flag3=flag2/2;
        flag4=flag3/2;
        flag5=flag4/2;
        sum=flag1+flag2+flag3+flag4;
        sum*=2;
        sum+=data;
        cout<<sum<<endl;
        cout<<flag5<<endl;
    }
    return 0;
}

发表于 2020-11-15 14:02:36 回复(1)
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()){
			int n =sc.nextInt();
			g(n);
		}
	}
	
	public static void g(int n){
		if(n<0){
			System.out.println(0);
		}
		double h5 = (double)n/32;
		double len = 0;
		for(int i=1;i<5;i++){
			len+=(n/Math.pow(2, i))*2;
		}
		len+=n;
        System.out.println(len);
		System.out.println(h5);
		
	}
}

发表于 2020-08-28 15:12:20 回复(0)
#include <iostream>
//输出第5次落地时经历来多少米,是不包括第五次可落地后回弹的高度的,
//总共经历的米数 == 最初的height + 中间每一次回弹高度*2
using namespace std;

int main()
{
    int height;
    while(cin >> height)
    {
        double res = height;
        int tmp = 2;
        for(int i = 1;i<=4;i++)
        {
            res += ((double)height/tmp)*2;
            tmp = tmp*2;
        }
        cout<<res<<endl<<(double)height/32<<endl;
    }
    
    
    
    
    return 0;
}

发表于 2020-07-16 10:09:53 回复(0)
#include<iostream>
using namespace std;
int main(){
    double height;
    while(cin>>height){
        double h = (double)height;
        double dis,h5;
        dis=h+h+(h/2)+(h/4)+(h/8);
        h5=h/32;
        cout<<dis<<endl;
        cout<<h5<<endl;
    }
    return 0;
}

发表于 2020-05-06 22:53:25 回复(0)
import sys


while True:
    # try:
    line = sys.stdin.readline().strip()

    if line == '':
        break

    import copy
    cur = float(copy.deepcopy(line))
    down = -cur
    for i in range(5):
        down += 2*cur
        cur = cur / 2

    print(down)
    print(cur)
编辑于 2020-03-24 19:37:59 回复(0)
规则大概是保留6位有效数字,多余的数字按照4舍6入5留双的规则,如果有.0,去掉小数点
真是服了这道题
具体算法比较原始,能过测试用例也就不再优化了(就是不知道有没有现成的函数)
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		while (scanner.hasNextDouble()) {
			int height = scanner.nextInt();

			double total = height;
			double curheight = height;
			for (int i = 1; i < 5; i++) {
				total = total + curheight;
				curheight = curheight / 2;
			}
			curheight = curheight / 2;
			try {
				System.out.println(sisheliuru(total));
				System.out.println(sisheliuru(curheight));
			} catch (Exception e) {
				System.out.println("error");
			}

		}
		scanner.close();
	}

		/**
	 * 保留6位有效数字,对后面的数字实行四舍六入五留双,如果double结果有.0,去掉小数点
	 * 
	 * @param total
	 * @return
	 */
	public static String sisheliuru(double total) {

		StringBuilder sBuilder = new StringBuilder();

		int htotal = (int) total;
		String shtotal = htotal + "";

		String stotal = total + "";
		if (total == (int) total) {
			stotal = shtotal;
		}

		int haspoint = stotal.indexOf(".");

		if (stotal.length() <= 6 || (stotal.length() == 7 && haspoint != -1)) {
			return stotal;
		}

		if (shtotal.length() > 6) {
			sBuilder.append(shtotal.subSequence(0, 5));
			int point6 = shtotal.charAt(5) - '0';
			int point7 = shtotal.charAt(6) - '0';

			if (point7 > 5) {
				sBuilder.append(point6 + 1);
			} else if (point7 < 5) {
				sBuilder.append(point6);
			} else {
				if (point6 % 2 == 0) {
					sBuilder.append(point6);
				} else {
					sBuilder.append(point6 + 1);
				}
			}
			for (int i = 0; i < shtotal.length() - 6; i++) {
				sBuilder.append("0");
			}
		} else if (shtotal.length() == 6) {
			sBuilder.append(shtotal.subSequence(0, 5));
			int point6 = shtotal.charAt(5) - '0';
			if (haspoint != -1) {
				int point7 = stotal.charAt(haspoint + 1) - '0';
				if (point7 > 5) {
					sBuilder.append(point6 + 1);
				} else if (point7 < 5) {
					sBuilder.append(point6);
				} else {
					if (point6 % 2 == 0) {
						sBuilder.append(point6);
					} else {
						sBuilder.append(point6 + 1);
					}
				}
			} else {
				sBuilder.append(point6);
			}
		} else {
			if (haspoint != -1) {
				sBuilder.append(stotal.subSequence(0, 6));
				int point6 = stotal.charAt(6) - '0';
				int point7 = stotal.charAt(7) - '0';
				if (point7 > 5) {
					sBuilder.append(point6 + 1);
				} else if (point7 < 5) {
					sBuilder.append(point6);
				} else {
					if (point6 % 2 == 0) {
						sBuilder.append(point6);
					} else {
						sBuilder.append(point6 + 1);
					}
				}
			} else {
				sBuilder.append(stotal);
			}
		}

		return sBuilder.toString();
	}
}

编辑于 2020-02-10 20:07:46 回复(0)
#include<iostream>
using namespace std;
double journey(double &begin){
    double total=begin;
    for(int i=1;i<5;i++){
        total+=begin;
        begin/=2;
    }
    begin/=2;
    return total;
}
int main(){
    int begin;
    while(cin>>begin){
        double height=(double)begin;
        double total=journey(height);
        cout<<total<<endl<<height<<endl;
    }
}

发表于 2020-01-06 22:33:20 回复(0)
//画图做题
#include<iostream>
using namespace std;
int main(){
    int a;
    while (cin >> a){
        cout << (a*1.0) + (a*1.0 * 2 / (1 << 1)) + (a*1.0 * 2 / (1 << 2)) + (a*1.0 * 2 / (1 << 3)) + (a*1.0 * 2 / (1 << 4)) << endl;
        cout << (a*1.0 / (1 << 5)) << endl;
    }
    system("pause");
    return 0;
}

发表于 2019-07-16 11:41:07 回复(0)
#include <iostream>
using namespace std;
int main() {     double h;     while (cin >> h)         cout << 23 * h / 8 << endl << h/32 << endl;
}
敢敢单单
发表于 2019-07-12 01:30:23 回复(0)
#include <iostream>
using namespace std;
double bounce(int height)
{
    double high=(double)height;
    for(int i=0;i<5;i++)
        high/=2;
    return high;
}
double Sum(int height)
{
    double high=(double)height;
    double sum=high;
    for(int i=0;i<4;i++)
    {
        sum+=high;
        high/=2;
    }
    return sum;
}
int main()
{
    int high;
    while(cin>>high)
    {
        cout<<Sum(high)<<endl;
        cout<<bounce(high)<<endl;
    }
    return 0;
}

发表于 2019-04-08 17:28:14 回复(0)
#include <bits/stdc++.h>

using namespace std;

int main()
{     double x;     while(cin>>x)     {         double s=x, h=0.5*x;         int cnt=1;         while(cnt<5)         {             s += 2*h;             h /= 2;             cnt++;         }         cout<<s<<endl;         cout<<h<<endl;     }     return 0;
}

发表于 2019-01-08 23:04:05 回复(0)
#include<iostream>
using namespace std;

double getJourney(double high)
{     double sum = high;     for (int i = 0; i < 4; i++)     {         high = 1.0 * high / 2;         sum += high * 2;     }     return sum;
}

double getTenthHigh(double high)
{     for (int i = 0; i < 5; i++)     {         high = high / 2.0;     }     return (double)high;
}
int main()
{     int height;     while (cin >> height)     {         cout << getJourney(height) << endl;         cout << getTenthHigh(height) << endl;     }     return 0;
}

发表于 2018-07-18 23:17:22 回复(0)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
    double h = 0.0;
    while(cin>>h)
    {
        double sumH = h;
        int n=4;
        while(n--)
        {
            sumH += h;
            h = h/2.0;
        }
        cout<<sumH<<endl;
        cout<<h/2.0<<endl;
    }
    return 0;
}

编辑于 2018-05-29 21:34:07 回复(4)
#include <stdio.h>
int main(){
    float n;
    while(scanf("%f",&n)!=EOF){
        float sum=n;
        int m=4; 
        while(m--){
            n/=2;
            sum+=n*2;
        }
        n/=2;
        printf("%g\n",sum);
        printf("%g\n",n);
    }
    return 0;
}

发表于 2018-02-22 14:16:53 回复(1)