当前位置:网站首页>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;
}
边栏推荐
- JS shift operators (< <,> > and > > >)
- [acnoi2022] guess numbers
- 使用vscode查看Hex或UTF-8编码
- 小项目(servelt+jsp+mysql+EL+JSTL)完成一个登录功能的Servlet,具有增删改查的操作。实现登录身份验证,防止非法登录,防止多点登录,记住用户名密码功能。
- RocksDB LRUCache
- 金属有机骨架MIL-88负载阿霉素DOX|叶酸修饰UiO-66-NH2负载阿霉素[email protected]纳米粒子
- JS new challenges
- Conversion function and explicit
- GoLand 2021.1.1: configure the multi line display of the tab of the open file
- Go: send the get request and parse the return JSON (go1.16.4)
猜你喜欢
FPGA测试方法以Mentor工具为例
消息订阅与发布
使用vscode查看Hex或UTF-8编码
Message subscription and publishing
Current situation, analysis and prediction of information and innovation industry
QT learning 19 standard dialog box in QT (top)
JS input number and standard digit number are compared. The problem of adding 0 to 0
Vite project commissioning
玖逸云黑免费无加密版本源码
Redis: redis data structure and key operation commands
随机推荐
JS shift operators (< <,> > and > > >)
Uniapp tips - scrolling components
Go language web development series 30: gin: grouping by version for routing
Similarities and differences of sessionstorage, localstorage and cookies
[Jilin University] information sharing of postgraduate entrance examination and re examination
Generate directories from web content
RocksDB LRUCache
金属有机骨架(MOFs)抗肿瘤药载体|PCN-223装载甲硝唑|UiO-66包载盐酸环丙沙星([email protected])
28:第三章:开发通行证服务:11:在配置文件中定义属性,然后在代码中去获取;
JS continues to explore...
page owner特性浅析
Go language web development series 29: Gin framework uses gin contrib / sessions library to manage sessions (based on cookies)
Qt学习18 登录对话框实例分析
Go language web development series 26: Gin framework: demonstrates the execution sequence of code when there are multiple middleware
Metal organic framework MOFs loaded with non steroidal anti-inflammatory drugs | zif-8 wrapped Prussian blue loaded quercetin (preparation method)
Leetcode-1175. Prime Arrangements
JVM runtime data area
Summary of common error reporting problems and positioning methods of thrift
Page generation QR code
如何使用lxml判断网站公告是否更新