HDU-1012,u Calculate e

简介: HDU-1012,u Calculate e

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 1


1 2


2 2.5


3 2.666666667


4 2.708333333


程序代码:


#include<stdio.h>
int f(int j)
{
  int i,sum=1;
  for(i=1;i<=j;i++)
    sum*=i;
  return sum;
}
int main()
{
  int i;
  double e,num=2.5;
  printf("n e\n");
  printf("- -----------\n");
  printf("0 1\n");
  printf("1 2\n");
  printf("2 2.5\n");
  for(i=3;i<=9;i++)
  {
    e=1.0*1/f(i);
    num+=e;
    printf("%d %.9f\n",i,num);
  }
  return 0;
}


相关文章
|
算法
uva 11549 CALCULATOR CONUNDRUM
题目链接 刘汝佳算法竞赛经典入门训练指南p42
41 0
|
算法 C++
uva11549Calculator Conundrum
题意:有一个老式计算器,只能保存最多n位数,如果结果超出n位,则保留前n位。现在输入一个n和一个k,k表示一个数字,然后不停的求k的平方并令k=k*k,发现会出现循环的结果,求所有结果种最大的一个。 分析:暴力模拟可以过的,但有更好的算法。
769 0
uva 11549 - Calculator Conundrum
点击打开链接uva 11549 思路:模拟 分析: 1 题目要求找到最大的n位数,那么我们通过不断的模拟,求出最大的ans 2 这里有个问题就是我们怎么知道什么时候结束呢?我们知道如果当前数已经有出现了那么说明刚好一个循环,这里利用m...
847 0
1001 Calculate a + b
#includeusing namespace std;int main(){ int x,y; while(cin>>x>>y){  cout
710 0