当前位置:网站首页>C语言入门实战(12):求自然常数e的值
C语言入门实战(12):求自然常数e的值
2022-07-27 02:18:00 【liberg】
这是《C语言入门实战》系列的第12篇。
上一篇:C语言入门实战(11):输入一组正整数,求逆序数的和
题目
利用算式:
e = 1 0 ! + 1 1 ! + 1 2 ! + . . . + 1 n ! + . . . e=\frac{1}{0!}+\frac{1}{1!}+\frac{1}{2!}+...+\frac{1}{n!}+... e=0!1+1!1+2!1+...+n!1+...
求自然常数e,要求控制精度在0.000001,请编程解决该问题。
要求
本程序无需输入,结果只输出e的值即可。
参考代码
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
float e = 1;//第1项,1/0!
float d = 1;
int i;
int pro = 1;//分母
for(i=1;d>=1e-6;i++) {
pro *= i;
d = (float)1/pro;
e += d;
}
printf("%f", e);//2.718282
return 0;
}
代码复盘
刚开始,e初始化为第一项的值,然后利用n! = n * (n-1)!的性质,for循环中依次计算从第二项开始各项的分母。
边栏推荐
- Solution to Chinese garbled code in console header after idea connects to database to query data
- The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode
- Explain工具实际操作
- 基于OpenCV的轮廓检测(1)
- Penetration test - post penetration - Trace cleaning
- Daffodils (day 78)
- Cocos game practice-04-collision detection and NPC rendering
- Redis源码学习(33),命令执行过程
- How to optimize MySQL
- Common weak password Encyclopedia
猜你喜欢

The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode

MySQL underlying data structure

Take you to know what Web3.0 is

flask_restful中reqparse解析器继承

If the detailed explanation is generated according to the frame code

复盘:图像有哪些基本属性?关于图像的知识你知道哪些?图像的参数有哪些

Network security / penetration testing tool awvs14.9 download / tutorial / installation tutorial

mysql底层数据结构

Contour detection based on OpenCV (1)

Worthington papain dissociation system solution
随机推荐
Code review pyramid
Fastboot刷机
Indexing best practices
Method of converting curtain article OPML into markdown
Banyan data model of Bairong
redis入门练习
数字孪生实际应用:智慧城市项目建设解决方案
榕树贷款,
[1206. Design skip table]
Tool class of localdatetime sorted out by yourself
NLP hotspots from ACL 2022 onsite experience
GetObject call timing of factorybean
mysql底层数据结构
Principle understanding and application of hash table and consistent hash
Realization of regular hexagon map with two-dimensional array of unity
关于使用hyperbeach出现/bin/sh: 1: packr2: not found的解决方案
mysql出现不存在错误
redis秒杀案例,跟着b站尚硅谷老师学习
Csu18m91 is used as the master controller of the intelligent scale scheme
MySQL underlying data structure