当前位置:网站首页>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循环中依次计算从第二项开始各项的分母。
边栏推荐
- ZJCTF_ login
- 复盘:图像有哪些基本属性?关于图像的知识你知道哪些?图像的参数有哪些
- What are "full five unique" and "full two unique"? Any difference?
- Tool class of localdatetime sorted out by yourself
- [common search questions] 111
- Spark Learning Notes (VI) -- spark core core programming RDD action operator
- flask_restful中reqparse解析器继承
- If the detailed explanation is generated according to the frame code
- 百融榕树数据分析拆解方法
- Database usage security policy
猜你喜欢

Characteristics and determination scheme of Worthington pectinase

Vector to SVG method

Cocos game practice-04-collision detection and NPC rendering

Introduction to database - Introduction to database

LPCI-252通用型PCI接口CAN卡的功能和应用介绍
![[tree chain dissection] template question](/img/6b/7ec6f36d5f2373aee163c2cb766b29.png)
[tree chain dissection] template question

Worthington papain dissociation system solution

Spark: calculate the average value of the same key in different partitions (entry level - simple implementation)

Do you really understand code rollback?
![[tree chain dissection] 2022 Hangzhou Electric Multi school 21001 static query on tree](/img/b3/58c053d082807a0e5639e5b968aa20.png)
[tree chain dissection] 2022 Hangzhou Electric Multi school 21001 static query on tree
随机推荐
Customer cases | pay attention to the elderly user experience, and the transformation of bank app to adapt to aging should avoid falsehood and be practical
Maximum continuous subsequence (day 77)
百融榕树数据分析拆解方法
明汯投资裘慧明:长期优异超额的背后考验的是团队的投研能力和策略的完整性
The application and significance of digital twins are the main role and conceptual value of electric power.
Contour detection based on OpenCV (2)
客户端发送一条sql如何与服务器交互
[1206. Design skip table]
Quick sequencing and optimization
Learning and understanding of four special data types of redis
Deployment of ruoyi's environment and operation of the system
网络安全/渗透测试工具AWVS14.9下载/使用教程/安装教程
Data analysis and disassembly method of banyan tree in Bairong
477-82(236、61、47、74、240、93)
The function and application of lpci-252 universal PCI interface can card
在typora中插入图片和视频
Binary tree (day 82)
Digital analog 1232
J-3-point practice in the second game of 2022 Niuke multi school
Banyan loan,