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
 

代码:
//  一道简单的递归题,需要0~9的阶乘,递归计算一下就行
// 输出的时候要注意保留9位小数,0也保留 #include <iostream>
#include<stdio.h>
using namespace std;
int fun[10];
double a[1000] = {0.0};
int g(int x)//递归求0--9的阶乘
{
int z;
if(x <= 1 )
z = 1;
else
z = g(x-1)*x;
return z;
}
void f()
{
a[0] = 1;
a[1] = 2;
for(int i = 2;i<10;i++)
{
a[i] = a[i-1]+(1/(double)g(i));
}
} int main()
{
f();
cout<<"n e"<<endl;
cout<<"- -----------"<<endl;
cout<<0<<" "<<a[0]<<endl;
cout<<1<<" "<<a[1]<<endl;
cout<<2<<" "<<a[2]<<endl;
for(int i = 3;i<10;i++)
{
printf("%d %11.9f\n",i,a[i]);
//cout.precision(10);
//cout<<i<<" "<<a[i]<<endl;
// C++的cout.precision(x);不保留0,要注意
} //cout<<f(n)<<endl; return 0;
}


ACM YTU 1012 u Calculate e的更多相关文章

  1. HDU 1012 u Calculate e(简单阶乘计算)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1012 u Calculate e Time Limit: 2000/1000 MS (Java/Oth ...

  2. HDU 1012 u Calculate e【暴力打表,水】

    u Calculate e Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  3. 杭电 1012 u Calculate e【算阶乘】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1012 解题思路:对阶乘递归求和 反思:前面3个的输出格式需要注意,可以自己单独打印出来,也可以在for ...

  4. hdu 1012:u Calculate e(数学题,水题)

    u Calculate e Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  5. hdoj 1012 u Calculate e

    u Calculate e Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  6. YTU 1012: A MST Problem

    1012: A MST Problem 时间限制: 1 Sec  内存限制: 32 MB 提交: 7  解决: 4 题目描述 It is just a mining spanning tree ( 最 ...

  7. ACM YTU 2018 母牛的故事

    母牛的故事 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  8. ACM YTU 十进制与八进制的转换 (栈和队列) STL栈调用

    十进制与八进制的转换(栈和队列) Description 对于输入的任意一个非负十进制整数,利用栈打印输出与其等值的八进制数. Input 111 Output 157 Sample Input 14 ...

  9. ACM YTU 挑战编程 字符串 Problem A: WERTYU

    Problem A: WERTYU Description A common typing error is to place yourhands on the keyboard one row to ...

随机推荐

  1. List迭代循环时出现分问题

    一个List,通过迭代之后给List中的实体重新赋值,代码如下 public List getListByPage(Page currPage) { Map recordTypeMap = BusnD ...

  2. replace empty char with new string,unsafe method和native implementation的性能比较

    1: class Program 2: { 3: static void Main(string[] args) 4: { 5: string s = File.ReadAllText(@" ...

  3. [SAM4N学习笔记]UART的使用

    一.准备工作:      将上一节搭建的工程复制一份,命名为"3.uart".这一节主要讲如何使用SAM4N的UART功能,实现串口的收发. 二.程序编写: 细心看数据手册的朋友也 ...

  4. 怎么通过IE连接本机oracle数据库

    这个目录下D:\oracle\product\10.2.0\db_study\install ,有个reademe.txt文件,里面记录着你访问数据库的网址和端口.

  5. windows上zend server安装 报The server encountered an internal error or misconfiguration and was unable to complete your request -解决方法 摘自网络

    windows上zend server安装完成后报如下错误:   Internal Server Error The server encountered an internal error or m ...

  6. yii2

    yii2框架官方说明文档 http://www.yiiframework.com/doc/guide/2.0/zh_cn/caching.page yii2创建您的第一个application应用 h ...

  7. LeetCode——Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  8. JS获取和设置光标的位置

    <html> <head> <script language="javascript"> function getCursortPosition ...

  9. Oracle约束操作

    约束的概念: 约束是在表中定义的用于维护数据库完整性的一些规则.通过为表中的字段定义约 束,可以防止将错误的数据插入到表中. 注意: 1.如果某个约束只作用于单独的字段,既可以在字段级定义约束,也可以 ...

  10. Android保存设置的PreferenceActivity

    界面XML文件:preference_setting.xml <?xml version="1.0" encoding="UTF-8"?> < ...