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.
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
这道题没输入啊。。。
对于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