hdu-1012 u Calculate e
2015-09-12 10:27
459 查看
题目来源:hdu-1012
题目大意:
给出一个e的表达式,即是求前n项的阶乘的导数之和。表达式为:e=1+1/1!+1/2!+1/3!+1/4!+……+1/9!;输出参考输出样例,依次输出n从0至9的结果。
解题思路:
首先按照输出及简单运算可知n等于0、1、2的时候对应结果为1、2、2.5;将此直接输出结果,此后结果含有多项小数,保留小数点后9位;对此可先求出i的阶乘,然后代入表达式,将运算结果输出即可。
AC代码:
u Calculate e Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 36307 Accepted Submission(s): 16399 Problem Description A simple mathematical formula for e is e=Σ1/i!(i=0~n); 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 Source Greater New York 2000
题目大意:
给出一个e的表达式,即是求前n项的阶乘的导数之和。表达式为:e=1+1/1!+1/2!+1/3!+1/4!+……+1/9!;输出参考输出样例,依次输出n从0至9的结果。
解题思路:
首先按照输出及简单运算可知n等于0、1、2的时候对应结果为1、2、2.5;将此直接输出结果,此后结果含有多项小数,保留小数点后9位;对此可先求出i的阶乘,然后代入表达式,将运算结果输出即可。
AC代码:
#include<stdio.h> double factorial(int n) //求阶乘函数 { double num=1; for(int i=1;i<=n;i++) num*=i; return num; } int main() { double sum; //对前面可直接计算出结果的单独输出 printf("n e\n- -----------\n0 1\n1 2\n2 2.5\n"); sum=2.5; for(int i=3;i<=9;i++) //将运算结果逐个计算并输出 printf("%d %.9lf\n",i,sum+=1/factorial(i)); return 0; }
相关文章推荐
- 服务器备份百度云
- Asp.net固定功能位充满了零(解决,演示样本)!
- LeetCode_OJ【14】Longest Common Prefix
- 数据挖掘(3):关联规则评价
- Leetcode 3 Longest Substring Without Repeating Characters
- Codeforces #319(Div.2) A. Multiplication Table
- 《剑指offer》第一个只出现一次的字符位置
- 【Java】_ 数组、哈希表、迭代器以及随机数生成器
- 数据挖掘(2):关联规则FpGrowth算法
- 数据挖掘(1):关联规则挖掘基本概念与Aprior算法
- 欢迎使用CSDN-markdown编辑器
- UISearchBar 的相关属性和delegate
- 报道了
- 软件开发项目管理心得(二)
- 定向爬取指定网页数据,并且过滤
- Eclipse安装Maven插件Maven Integration for Eclipse
- 软件开发项目管理心得(一)
- 常用机器学习方法总结
- hdu acm 2602 Bone Collector
- nyist 468 Fibonacci数列(六)(Miller-Rabin算法 大数素性测试)