首页 > 试题广场 >

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

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

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


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


输入描述:

输入起始高度,int型



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

输入

1

输出

2.875
0.03125
while True:
  try:
    a,b = int(input()),[]
    for i in range(5):
      # 第一次下落
      b.append(a)
      a = a/2
      # 反弹
      b.append(a)
    print("%.3f" %(sum(b)-b[-1]))
    print(round(b[-1],5))
  except:
    break

发表于 2020-02-19 19:53:24 回复(0)

/*使用递归解*/
#include<stdio.h>
#include<stdlib.h>

double journey(double high){
    static int cnt_j = 0;
    cnt_j++;
    if(cnt_j >= 5){
        cnt_j = 0;
        return high;
    }
    return (high + high / 2) + journey(high/2);
}

double tentHigh(double high){
    static int cnt_h = 0;
    cnt_h++;
    if(cnt_h >= 5){
        cnt_h = 0;
        return high/2;
    }
    return tentHigh(high / 2);
}

double getJourney(int high)
{
    return journey((double)high);
}

double getTenthHigh(int high)
{
    return tentHigh((double)high);
}


int main(){
    int high = 0;
    while(~scanf("%d",&high)){
        printf("%g\n",getJourney(high));
        printf("%g\n",getTenthHigh(high));
    }
}


发表于 2019-11-01 19:17:59 回复(1)
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)
傻傻的写了半天循环,后来才想到这么简单~
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            Double h = sc.nextDouble();
            System.out.println(h*2.875);
            System.out.println(h*0.03125);
        }
    }
}


发表于 2020-07-22 11:43:28 回复(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)

问题信息

难度:
494条回答 36741浏览

热门推荐

通过挑战的用户

查看代码