HDU 1012 u Calculate e

来源:互联网 发布:linux ping不通百度 编辑:程序博客网 时间:2024/06/05 00:43
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 11 22 2.53 2.6666666674 2.708333333
题意即为输出n从0~~9所对应的e的值
#include<stdio.h>#include<string.h>#include<math.h>#include<iostream>#include<algorithm>#include<string>using namespace std;int main(){    double e,n;    cout<<"n"<<" "<<"e"<<endl;;    cout<<"- -----------"<<endl;    cout<<"0 1"<<endl;    cout<<"1 2"<<endl;    cout<<"2 2.5"<<endl;    e=2.5;    for(int i=3; i<=9; i++)    {        n=1;        for(int j=1; j<=i; j++)            n*=j;        e+=1/n;        printf("%d %.9f\n",i,e);    }}
0 0
原创粉丝点击