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

来源:互联网 发布:武汉ui知乎 编辑:程序博客网 时间:2024/05/19 19:34

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 11 22 2.53 2.6666666674 2.708333333
看到前三个输出是一个有限小数,而且位数没有后面的多,于是考虑直接先输出前面的内容,再计算后面的内容。
一定要注意这个提示:n e- -----------0 11 22 2.53 2.6666666674 2.7083333335 2.7166666676 2.7180555567 2.7182539688 2.7182787709 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++; } } 


0 0
原创粉丝点击