当前位置:网站首页>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 = 0
double 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;
}
边栏推荐
- Qt学习20 Qt 中的标准对话框(中)
- 7-9 find a small ball with a balance
- Implementation of Muduo accept connection, disconnection and sending data
- JS first summary
- How to bold text in AI
- [combinatorics] permutation and combination (examples of combinatorial number of multiple sets | three counting models | selection problem | combinatorial problem of multiple sets | nonnegative intege
- Dynamic programming 01 knapsack and complete knapsack
- Invalid Z-index problem
- Exercise 6-2 using functions to sum special A-string sequences
- GoLand 2021.2 configure go (go1.17.6)
猜你喜欢
Failure of vector insertion element iterator in STL
Qt学习23 布局管理器(二)
Exercise 6-2 using functions to sum special A-string sequences
Common network state detection and analysis tools
JVM family - overview, program counter day1-1
Go: send the get request and parse the return JSON (go1.16.4)
Go language unit test 5: go language uses go sqlmock and Gorm to do database query mock
Go language web development series 28: solve cross domain access of CORS with gin contrib / CORS
3D vision - 2 Introduction to pose estimation - openpose includes installation, compilation and use (single frame, real-time video)
QT learning 25 layout manager (4)
随机推荐
Cross linked cyclodextrin metal organic framework loaded methotrexate slow-release particles | metal organic porous material uio-66 loaded with flavonoid glycosides | Qiyue
[ACNOI2022]猜数
Go language web development series 26: Gin framework: demonstrates the execution sequence of code when there are multiple middleware
QT learning 25 layout manager (4)
Vite project commissioning
How to bold text in AI
“又土又穷”的草根高校,凭什么被称为“东北小清华”?
Back to top implementation
Go 1.16.4: manage third-party libraries with Mod
FPGA测试方法以Mentor工具为例
好看、好用、强大的手写笔记软件综合评测:Notability、GoodNotes、MarginNote、随手写、Notes Writers、CollaNote、CollaNote、Prodrafts、Noteshelf、FlowUs、OneNote、苹果备忘录
Uniapp tips - set background music
Metal organic framework (MOFs) antitumor drug carrier | pcn-223 loaded with metronidazole | uio-66 loaded with ciprofloxacin hydrochloride(
Metal organic framework material zif-8 containing curcumin( [email protected] Nanoparticles) | nano metal organic framework carry
消息订阅与发布
3D vision - 2 Introduction to pose estimation - openpose includes installation, compilation and use (single frame, real-time video)
Go language web development series 25: Gin framework: using MD5 to verify the signature for the interface station
Duet date picker (time plug-in that can manually enter the date)
concat和concat_ws()区别及group_concat()和repeat()函数的使用
Common network state detection and analysis tools