当前位置:网站首页>After learning the loop, I came across the problem of writing factorial of N, which caused a series of problems, including some common pitfalls for beginners, and how to simplify the code
After learning the loop, I came across the problem of writing factorial of N, which caused a series of problems, including some common pitfalls for beginners, and how to simplify the code
2022-07-28 01:53:00 【strong】
1. Calculation n The factorial
2. Calculation 1!+2!+3!…+n!
1 Calculation n The factorial
#include <stdio.h>
int main()
{
// Input
int n = 0;
scanf("%d", &n);
// produce 1~n The number of
int i = 0;
int ret = 1;
for (i = 1; i <= n; i++)
{
ret = ret * i;
}
printf("%d\n", ret);
return 0;
}

This is very simple , Things that can be solved in one cycle . Let's look at the train of thought ,n The factorial ( Here 3 give an example )3 The factorial of is 123 , Let's give a cycle , produce 1 To 3 Number of numbers , Then multiply it by the variable you define ret in ( Note that there ret The initial value of can only be set to 1, Initial value if 0, Then any number multiplied by the past is 0 了 , You know what )
2 Calculation 1!+2!+3!…+n!
2.1 Look at the code that hides the pit
#include <stdio.h>
int main()
{
// Input
int n = 0;
scanf("%d", &n);
int i = 0;
int ret = 1;
int sum = 0;
int j = 0;
for (j = 1; j <= n; j++)
{
for (i = 1; i <= j; i++)
{
ret = ret * i;
}
sum = sum + ret;
}
printf("%d\n", sum);
return 0;
}
Look at the results 
Here it comes out 1 To 3 The sum of the factorials of is 15,, But actually it should be 9. What's the reason ? Many people are the result of the first question above , Simply and roughly put a loop on the outside and add them up . But the problem is , Namely When the inner circulation is carried out once , When entering ret Is initialized to 1, Lead to calculation 3 When we take the factorial of the ,ret The initial value is 2, So it leads to mistakes
2.2 Point out the pit , And give the correct code
#include <stdio.h>
int main()
{
// Input
int n = 0;
scanf("%d", &n);
int i = 0;
int ret = 1;
int sum = 0;
int j = 0;
for (j = 1; j <= n; j++)
{
ret = 1;// The solution is to add this sentence !!!
for (i = 1; i <= j; i++)
{
ret = ret * i;
}
sum = sum + ret;
}
printf("%d\n", sum);
return 0;
}

2.3 Code simplification
First, simplify the code
#include <stdio.h>
int main()
{
// Input
int n = 0;
scanf("%d", &n);
int ret = 1;
int sum = 0;
int j = 0;
for (j = 1; j <= n; j++)
{
ret = ret * j;
sum = sum + ret;
}
printf("%d\n", sum);
return 0;
}
Observe carefully 2.2 Code for , You will find that in these two cycles , A lot of things have been calculated repeatedly , So in simplified code , It can be adjusted .
Here are some examples to help understand , At the end of the calculation 1 After the factorial of ,2 The factorial of only needs to be in 1 On the basis of factorial of 2 Just go , And so on ,3 The factorial of is 2 The result of the factorial of is multiplied by 3. So after giving up a cycle , Code efficiency has greatly improved
3 Summary
The content has been written in detail in the body , If you have any questions or suggestions , Welcome to communicate with me . Here is the sturdy learning blog , Thank you for your support
边栏推荐
猜你喜欢

Linux系统彻底删除Mysql

If you are still using WiFi, you will be out: li-fi is better!!!

【taichi】在太极中画出规整的网格

Stock problems 5 times

Real time synchronization and conversion of massive data based on Flink CDC

Leetcode 2341. How many pairs can an array form

BGP联邦实验

企业运维实践-使用Aliyun容器镜像服务对海外gcr、quay仓库镜像进行镜像拉取构建

石油化工行业迎战涨价大潮,经销商分销系统平台数字化赋能经销商与门店

Qt 绘制系统简介
随机推荐
以“数字化渠道”撬动家用电器消费蓝海,经销商在线系统让企业生意更进一步
EEG多元模式分析预测慈善捐赠行为
腾讯云HiFlow场景连接器
Oracle Rac 集群文件目录迁移
GBase 8c 注释信息函数
26.抽象化和模板思想
【面试:并发篇28:volatile】有序性
存储成本降低 80%,有赞数据中台成本治理怎么做的?
"Do you" want to be a test / development programmer? We strive to sprout
2.2 comprehensive application questions - sequence table
HCIP第十五天
面试官:你确定Redis是单线程的进程吗?
ros2 launch文件常用模块
HRD 1. a simple and reliable HRD detection method
Prediction of charitable donation behavior by EEG multivariate model analysis
The story of the third uncle
Docker builds MySQL master-slave locally
Mark's story
一些事情的思考
GBase 8c 事务ID和快照(三)