您的位置:首页 > 其它

HDOJ 1012 u Calculate e

2013-02-16 20:38 183 查看


u Calculate e

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 21436 Accepted Submission(s): 9440



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


Source

Greater New York 2000

这道题没输入啊。。。

对于C++来说,cout比较麻烦吧,真心有点想用printf了。

#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
cout<<"n e"<<endl<<"- -----------"<<endl<<"0 1"<<endl
<<"1 2"<<endl<<"2 2.5"<<endl;;
double a=2.5; int ii=2;
cout.precision(9);
for(int i=3;i<10;++i){
ii*=i;
a+=1.0/ii;
cout<<i<<' '<<fixed<<a<<endl;
}
return 0;
}


百度上搜到的cout控制符。。感谢原作者

cout格式输出:
控制符描 述
dec置基数为10
hex置基数为16
oct置基数为8
setfill(c)设填充字符为c
setprecision(n)设显示小数精度为n位
setw(n)设域宽为n个字符
setiosflags(ios::fixed)固定的浮点显示
setiosflags(ios::scientific)指数表示
setiosflags(ios::left)左对齐
setiosflags(ios::right)右对齐
setiosflags(ios::skipws)忽略前导空白
setiosflags(ios::uppercase)16进制数大写输出
setiosflags(ios::lowercase)16进制数小写输出
resetiosflags(ios::*)终止已设置的输出格式状态
此外,还可等价地通过调用输出流对象cout的成员函数来控制输出格式:

cout.precision(n) = setprecision(n)

cout.width(n) = setw(n)

cout.fill(c) = setfill(c)

cout.setf(ios::*) = setiosflags(ios::*)

cout.unsetf(ios::*) = resetiosflags(ios::*)
kdwycz的网站: http://kdwycz.com/

kdwyz的刷题空间:http://blog.csdn.net/kdwycz
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: