码迷,mamicode.com
首页 > 其他好文 > 详细

刷HDOJ 的心得体会

时间:2016-05-31 18:59:19      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:

今天,刷了HDOJ 1012(据说是水题)

u Calculate e

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 41083    Accepted Submission(s): 18688


Problem Description
A simple mathematical formula for e is

技术分享

where n is allowed to go to infinity. This can actually yield very accurate approximations of e using relatively small values of n.
 

 

Output
Output the approximations of e generated by the above formula for the values of n from 0 to 9. The beginning of your output should appear similar to that shown below.
 

 

Sample Output
n e - -----------
0 1
1 2
2 2.5
3 2.666666667
4 2.708333333
 
-------------分隔线-----------------------------------------------------------
 

看了网上的一些代码,发现几乎所有人都在 i=8 时进行了这样的处理:

if (i == 8)
{

  cout << i << " " << std::setprecision(9) << sum << "0" << endl;
}


else
{
  cout << i << " " << std::setprecision(10) << sum << endl;
}

刚开始我不明白,于是,做了一个实验,去掉这句判断得到的结果:

 

技术分享

发现,在i = 8 的时候,少了一个0,原来上面的判断功能是这个(我还是不知道为什么),算是学习了一下。

还有:对于 setprecision() 函数的用法一直不是很清楚,通过这个明白,它的头文件为 #include<iomanip>

 

precision()函数设置或返回当前要被显示的浮点变量的位数。例如,下面的代码:

    float num = 314.15926535;
    cout.precision( 5 );
    cout << num;

displays

    314.16

一句话:就是控制输出整数和小数的总位数 !

刷HDOJ 的心得体会

标签:

原文地址:http://www.cnblogs.com/Tony-Liang/p/5546860.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!