题解 | #求小球落地5次后所经历的路程和第5次反弹的高度#
求小球落地5次后所经历的路程和第5次反弹的高度
https://www.nowcoder.com/practice/2f6f9339d151410583459847ecc98446
#include <stdio.h>
#include <math.h>
#include <string.h>
int main() {
int n = 0;
float nf = 0;
scanf("%d",&n);
nf = (float) n;
float len = nf *(pow(0.5, 0) + 2*pow(0.5, 1) + 2*pow(0.5, 2) + 2*pow(0.5, 3) + 2*pow(0.5, 4));
float hf = nf*pow(0.5,5);
char hfstr[16] = {0};
int hfstrLen = 0;
sprintf(hfstr, "%f", len);
hfstrLen = strlen(hfstr);
for(int i = hfstrLen; i >0; i--)
{
if('0' == hfstr[i-1])
hfstr[i-1] = 0;
else
break;
}
printf("%s\n%1.5f",hfstr,hf);
return 0;
}
