当前位置:网站首页>Two ways to find the factorial of n
Two ways to find the factorial of n
2022-08-09 17:48:00 【I'm free*】
Table of Contents

1. Method 1
Use a non-recursive approach
n!=1*2*3*...*n; Using for can solve this problem
//non-recursive#include int main(){int i = 0;int n = 0;int sum = 1;//sum is initialized to 1, not 0scanf("%d", &n);for (i = 1;i <= n;i++){sum = sum * i;}printf("%d!=%d", n, sum);return 0;} 2. Method 2
adoptedRecursive method
Text Explanation:
//Recursive
//n!=1*2*3*...*n
//get(1*2*3*...(n-1)) * n
//get(1*2*3*...(n-2))*(n-1)* n
//...
//get(1*2) 3* ... *(n-2) *(n-1)* n
//1*2...*(n-1)*n
Image explanation:
Example: Find the factorial of 5p>
Complete code map:

Analytical Diagram:

#include int get(int n){if(n>1){return n * get(n - 1);}else if(n == 1){return 1;}}int main(){//int n = 0, sum = 0;printf("Please enter a number: ");scanf("%d", &n);sum = get(n);printf("%d!=%d", n, sum);return 0;} 
边栏推荐
猜你喜欢

前缀和相关:区域和检索 - 数组不可变、二维区域和检索 - 矩阵不可变...

同步锁synchronized追本溯源

opacity和rgba的区别

WinServer 2019 组策略删除本地管理员且只允许域用户登陆

传输层协议TCP/UDP

重启网卡提示Bringing up interface eth0: Device eth0 does not seem to be present,delaying initialization.

继承关系下构造方法的访问特点

排序相关:数组的相对排序、最小的k个数(快排)、合并区间、翻转对 ...

三栏布局:左右固定宽,中间自适应的几种方式

com.ctc.wstx.exc.WstxParsingException: Illegal character entity: expansion character
随机推荐
Zip包的读取与写入
字典树、并查集相关:实现Trie、搜索推荐系统、朋友圈、被围绕的区域(未做) ...
2022深圳杯D题思路:复杂水平井三维轨道设计
Codeforces Round #808 (Div. 2)||沉淀
2021深圳杯A题思路 火星探测器着陆控制方案
String的构造方法,其他方法
学习编程的第二天
(十)打包和项目部署
3. Using Earth Engine Data
js和jq实现点击小眼睛后密码显示与隐藏切换
2022高教社杯 国赛数学建模 D题思路
Redis learning (1. Data structure in redis)
canvas学习(一)
Tracert 命令穿越防火墙不显示星号*的方法
布隆过滤器及LRU Cache的实现
Go语言基础(十二):并发编程
学习编程的第三天
QT程序设计多人聊天室(基于QT、sqlite3、TCP/IP)
2. Creating Interactive Maps
Win10 Runas 命令 域用户以管理员权限运行