zoj 1113 u Calculate e(小数点精度保留)

Background
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.

Example

Output

n e
- -----------
0 1
1 2
2 2.5
3 2.666666667
4 2.708333333

看到前三个输出是一个有限小数,而且位数没有后面的多,于是考虑直接先输出前面的内容,再计算后面的内容。 
一定要注意这个提示: n e - ----------- 0 1 1 2 2 2.5 3 2.666666667 4 2.708333333 5 2.716666667 6 2.718055556 7 2.718253968 8 2.718278770 9 2.718281526 这是AC的数据,注意n=8的情况,最后面无意义的0还是要保留。所以n=0、1、2作为特殊值输出,剩下的用%.9lf即可,%.10lg就要WA了
 
#include<stdio.h>
 void main()
{
	double arr[10] = {1};

	int i = 1,j = 3;

	while(i < 10)

	{

		arr[i] = i * arr[i - 1];

		i++;

	}
	printf("n e\n");
 	printf("- -----------\n");

	printf("0 1\n");

	printf("1 2\n");
	printf("2 2.5\n");
 	double result = 2.5;
	while(j < 10)
	{

		result = result + 1/arr[j];

 		printf("%d %11.9f\n",j,result);
 		j++;
 	}
 } 



你可能感兴趣的:(ZOJ,高精度,HDU)