当前位置:网站首页>Exercise 10-2 recursive factorial sum
Exercise 10-2 recursive factorial sum
2022-07-03 14:09:00 【ᯤ⁹ᴳ⁺ ·】
exercises 10-2 Recursively sum factorials (15 branch )
This problem requires a simple function to calculate the factorial of non negative integers , And use this function to find 1!+2!+3!+...+n! Value .
Function interface definition :
double fact( int n );
double factsum( int n );
function fact Should return n The factorial , It is suggested to use recursion to realize . function factsum Should return 1!+2!+...+n! Value . The problem is to ensure that the input and output are within the double precision range .
Sample referee test procedure :
#include <stdio.h>
double fact( int n );
double factsum( int n );
int main()
{
int n;
scanf("%d",&n);
printf("fact(%d) = %.0f\n", n, fact(n));
printf("sum = %.0f\n", factsum(n));
return 0;
}
/* Your code will be embedded here */
sample input 1:
10
sample output 1:
fact(10) = 3628800
sum = 4037913
sample input 2:
0
sample output 2:
fact(0) = 1
sum = 0double fact( int n ){
double f;
if(n>1){
f=n*fact(n-1);
}else{
f=1;
}
return f;
}
double factsum( int n ){
double sum=0;
for(int i=1;i<=n;i++){
sum+=fact(i);
}
return sum;
} 
边栏推荐
- 项目协作的进度如何推进| 社区征文
- How to use lxml to judge whether the website announcement is updated
- Go 1.16.4: manage third-party libraries with Mod
- GoLand 2021.2 configure go (go1.17.6)
- QT learning 21 standard dialog box in QT (Part 2)
- 玖逸云黑免费无加密版本源码
- page owner特性浅析
- How to delete an attribute or method of an object
- Uio-66-cooh loaded bendamostine | hydroxyapatite (HA) coated MIL-53 (FE) nanoparticles | baicalin loaded manganese based metal organic skeleton material
- Implementation of Muduo asynchronous logging
猜你喜欢

RocksDB LRUCache

Go language web development series 29: Gin framework uses gin contrib / sessions library to manage sessions (based on cookies)

八大排序

QT learning 25 layout manager (4)
[email protected])|制备路线"/>叶酸修饰的金属-有机骨架(ZIF-8)载黄芩苷|金属有机骨架复合磁性材料([email protected])|制备路线

Qt学习25 布局管理器(四)
![[Jilin University] information sharing of postgraduate entrance examination and re examination](/img/1d/550a991385b842a21e2b301725407e.png)
[Jilin University] information sharing of postgraduate entrance examination and re examination

How to use lxml to judge whether the website announcement is updated

Go language web development series 25: Gin framework: using MD5 to verify the signature for the interface station

核酸修饰的金属有机框架药物载体|PCN-223金属有机骨架包载Ad金刚烷|ZIF-8包裹阿霉素(DOX)
随机推荐
[combinatorics] permutation and combination (two counting principles, examples of set permutation | examples of set permutation and circle permutation)
QT learning 22 layout manager (I)
[ACNOI2022]猜数
QT learning 23 layout manager (II)
concat和concat_ws()区别及group_concat()和repeat()函数的使用
Qt学习21 Qt 中的标准对话框(下)
消息订阅与发布
[combinatorics] permutation and combination (examples of combinatorial number of multiple sets | three counting models | selection problem | combinatorial problem of multiple sets | nonnegative intege
QT learning 19 standard dialog box in QT (top)
Programmable logic device software testing
Implementation of Muduo accept connection, disconnection and sending data
Vite project commissioning
js . Find the first palindrome string in the array
Too many files with unapproved license
JVM object lifecycle
Another industry has been broken by Chinese chips. No wonder the leading analog chip companies in the United States have cut prices and sold off
QT learning 25 layout manager (4)
Go 1.16.4: manage third-party libraries with Mod
Analysis of the characteristics of page owner
Use vscode to view hex or UTF-8 codes