指尖上的代码[C语言版]-<2>

<2> 一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?

分析:


代码:

#include "Stdio.h"#include "Conio.h"#include "Math.h"int main(void){  /* init_h表示初始高度  sum用来记录球所经过的距离  h表示球反弹后的距离*/  float init_h=100,sum=0.0,h;  /* n表示球第n次着地*/  int n;  for(n=1;n<=10;n++)     {       if(n==1)         {           sum=100.0;         }       else         {            sum=sum+2*h;         }       h=init_h/(pow(2,n));     }  printf("The distance of the ball run is %fm.\n\n",sum);  printf("Tenth ball where the height is %fm.",h);  return 0;}

编译结果:

点石成金  写于  2012/08/07/14:21

猜你喜欢

转载自leili.iteye.com/blog/1624579