D - u Calculate e
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
大致意思:
精度逐渐提升到9的输出e的近似值(古怪的格式)
钻个小空子,前面不统一的地方直接输出出来
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
printf("n e\n- -----------\n0 1\n1 2\n2 2.5\n");
int i;
double a[10]={1,2,2.5},mul=2;
for (i=3; i<10; i++) {
mul *= i;
a[i] = a[i-1] + 1/mul;
printf("%d %.9f\n", i, a[i]);
}
return 0;
}