Zhejiang version of "C Programming Language (3rd Edition)" title set 4-8 exercises falling ball (20 points)

Here Insert Picture Description
Solution: analog, Distance = Distance - rebound , entitled distance of the n-th floor , so the rebound is not added.

#include <stdio.h>
int main()
{
    int n;
    double h, distance, rebound;
    scanf("%lf %d", &h, &n);
    distance = 0, rebound = 0; //n为0的时候,while不执行,所以这两个一定要赋初值。
    while (n--)
    {
        rebound = h / 2;                   //反弹高度等于目前高度的一半。
        distance = distance + h + rebound; //每弹一次距离为高度+反弹。
        h = rebound;                       //新一轮高度等于上一轮反弹。
    }
    printf("%.1lf %.1lf", distance - rebound, rebound);
    return 0;
}
Published 165 original articles · won praise 117 · Views 7802

Guess you like

Origin blog.csdn.net/qq_44458489/article/details/105320703