请教一个C++的代码

来源:百度知道 编辑:UC知道 时间:2024/06/15 13:36:48
请问下 题如何编程?

小球从100m高度自由落下,每次落地后反跳回原高度的一半,再落下。求它在第几次弹起时高度小于0.02米,到此次落地时共经过多少米?

#include<iostream>
using namespace std;
void main()
{
int n=0;
float h=10,sum=0;
while(h>=0.02)
{
n++;
sum+=1.5*h;
h=0.5*h;
}
cout<<"走过的路程为 "<<sum<<endl<<"跳起的次数为 "<<n<<endl;}

//球体落地
#include<iostream.h>
void main()
{
double h1=100,h2=100,sum=0.0;//h1开始高度 h2每次弹跳经过的距离 sum总距离
int i;
for(i=1;h1>0.02;i++)
{

sum+=h2;//加上上次移动距离
h1=h1/2.0;//每次谈起距离减半
h2=h1*2;//每次上升回落因此每次经过的距离要乘2
}
cout<<"sum="<<sum<<" "<<"h1="<<h1<<endl;//输出
cout<<i<<endl;//输出弹跳次数
}

#include <iostream>
using namespace std;
void main()
{
double a=100,sum=0;
int i=0;
do
{
a=a/2;
i++;
sum+=100+a*2;
}while(a>=0.02);
cout<&l