小球下落

小球从n米高处自由下落,每次弹起的高度是下落高度的70%,
当小球弹起的高度不足原高度的千分之一时,小球很快会停止跳动,
请计算小球在整个弹跳过程中所经历的总路程
(忽略弹起高度不足原高度千分之一的部分)。
(注:本题计算过程请用双精度实数)

 1 #include<iostream>
 2 #include<iomanip>
 3 using namespace std;
 4 int main()
 5 {
 6     double H;
 7     cin>>H;
 8     double h0=H*0.7,s=H;
 9     while(h0>=H/1000)
10     {        
11         s+=2.0*h0;
12         h0*=0.7;
13     }
14     cout<<setiosflags(ios::fixed)<<setprecision(4)<<s<<endl;  //保留4位小数 
15     return 0;
16 }

注意cout的小数点位数的控制输出!!!

可参考http://upliu.net/how-cout-out-2-precision.html

原文地址:https://www.cnblogs.com/fengyanlover/p/4940596.html