The ball falls freely from a given height, rebounds to half of its original height after hitting the ground, falls again, rebounds again, and so on. Asked n
how much distance did the ball travel in the air for the first time it lands? n
What is the height of the first rebound?
Input format:
Input two non-negative integers in one line, which are the initial height of the ball and the sum n
, both of which are within the range of long integers.
Output format:
In a row, output the distance that the ball passes in the air for the nth landing and n
the height of the bounce for the first time, separated by a space and one decimal place. The title guarantees that the calculation result does not exceed the double precision range.
Input sample:
33 5
Sample output:
94.9 1.0
Code:
# include <stdio.h>
# include <stdlib.h>
int main() {
int i = 1;
double height,n;
scanf("%lf %lf",&height,&n);
double km = height,km_n;
if (n == 0) {
printf("%.1lf %.1lf",0,0);
}else {
while(i < n) {
height *= 0.5;
km += (height * 2.0);
i += 1;
}
km_n = height * 0.5;
printf("%.1lf %.1lf",km,km_n);
}
return 0;
}
Submit screenshot:
Problem-solving ideas:
n=0
Judgment when paying attention !