指尖上的代码[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

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值