Kodewick
Empieza a aprender →
Básico←→0 / 50

La pelota que rebota

#025Básicototal += 2 * h6 min

Una pelota cae desde height metros; cada vez que toca el suelo, rebota a la mitad de la altura anterior y vuelve a caer. Escribe total_distance(height, times): al tocar el suelo por vez número times, ¿cuántos metros ha recorrido en total? Por ejemplo, desde 100 m: al primer bote ha recorrido 100 m; sube a 50 m y baja, y al segundo bote lleva 200 m.

Ejemplos
total_distance(100, 10)→299.609375
total_distance(100, 1)→100.0
total_distance(100, 3)→250.0
La funcióndouble total_distance(double height, int times)
main.c
double total_distance(double height, int times) {
    double total = height;
    double h = height / 2;
    for (int i = 2; i <= times; i++) {
        // tu código aquí: suma lo que sube y baja esta vez, y luego reduce a la mitad la altura del rebote
    }
    return total;
}
Ctrl + Enter también ejecuta
Resultado

Ejecutar prueba los ejemplos. Cuando salgan bien, Enviar.