[Algorithm question] Huawei computer test (Niu Ke) - HJ38 Find the distance experienced by the ball after landing 5 times and the height of the fifth rebound (Java)

Problem Description

Question
Suppose a ball falls freely from any height, and bounces back to half of its original height each time it hits the ground; when it falls again, how many meters did it experience when it landed for the fifth time? How high is the fifth rebound?
Data range: The initial height of the input ball satisfies 1≤ n ≤1000 and is guaranteed to be an integer.
Input description:
input the starting height, int type
output description:
respectively output the total number of meters and the height of the fifth rebound when the fifth landing is made.
Note: You can think that you can pass this question if you output the result with six or more decimal places.

Example
Input: 1
Output: 2.875
           0.03125

Solution

package huawei;
import java.util.Scanner;
public class Main{
    
    
	
    public static void main(String[] args){
    
    
    	Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
    
    
        	double input = sc.nextInt();
        	double high = input;
        	for(int i = 0; i < 4; i++){
    
    
        		high += input;
        		input /= 2.0;
        	}
        	input /= 2.0;
        	System.out.println(high);
        	System.out.println(input);
        	
        }
    }
} 

        The code was written by a buddy above Niuke, and it is used here as an example. When we encounter this kind of problem, most of the first thoughts are to solve it with a loop, just like the code above. In fact, there is a good solution to this problem, the specific code is as follows:

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner input = new Scanner(System.in);
        int height=input.nextInt();
        double num1=height*Math.pow(0.5,5);
        double num2=3*height-4*num1;
        System.out.println(num2);
        System.out.println(num1);
    }
}

problem solving ideas

        This problem is essentially an algorithm problem about geometric sequences, which can be solved by using the general term formula and summation formula of geometric sequences. Take this question as an example:
        Suppose the ball falls from a height of n, and bounces back to half of the original each time, then the first bounce is n 0.5, and the second is n 0.5*0.5, and so on , which is the geometric sequence, and the common ratio is 0.5.
        Note: In this question, the first item is the initial height, but the fifth rebound height is the sixth item in the sequence.
        According to the formula of the general item of the geometric sequence a n =a 1 *q n-1, the height of the fifth rebound can be calculated and converted into code as follows:

		double num1=height*Math.pow(0.5,5);

        The number of meters experienced during the fifth landing is calculated by transforming the geometric sequence. The process idea is as follows: According to the summation
        formula of the geometric sequence S n =(a 1 -a n q)/(1-q) The total height of the five rebounds of the ball can be calculated, which is the height of the five green lines in the picture below. If we assume that after the fifth bounce, it will fall to the ground (there is a red line after the x in the figure, which is not drawn in the figure), then it will be better calculated. The total length is 2 of the sum of the first five terms of the geometric sequence Times, note that the first item here is the position of the first bounce. Assuming that the initial height is x, then the height of the first rebound is x/2, and its sum common ratio q (0.5) is substituted into the formula:

S5=(x/2-a5*0.5)/(1-0.5)*2

insert image description here
After the calculation, the records that fell at the beginning should be counted. Because the title requires counting until the 5th landing, there is no rebound at that time, so subtract twice the distance of the 5th rebound, the total distance is as follows:

L=S 5 +x-2a 5 Because num1 in the previous code is a 5
here , so after bringing in all of them, we get: L=(x 0.5-num1 0.5) 2 2-num1 2+x; after simplification, L= The code corresponding to the result of 3 x-4*num1 is:



		double num2=3*height-4*num1;

Acho que você gosta

Origin blog.csdn.net/m0_73845616/article/details/131915020
Recomendado
Clasificación