leili 发表于 2013-1-26 12:28:35

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

<2> 一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?
分析:
http://my.csdn.net/uploads/201208/07/1344320693_5452.jpg

代码:

#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;}
编译结果:
http://my.csdn.net/uploads/201208/07/1344320843_2903.jpg
点石成金  写于  2012/08/07/14:21
页: [1]
查看完整版本: 指尖上的代码[C语言版]-<2>