当前位置:网站首页>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;
}
边栏推荐
- TS code automatically generates JS
- 怎样删除对象的某个属性或⽅法
- 可编程逻辑器件软件测试
- GoLand 2021.1: rename the go project
- How to promote the progress of project collaboration | community essay solicitation
- Vite project commissioning
- Too many files with unapproved license
- 解决MySql 1045 Access denied for user ‘root‘@‘localhost‘ (using password: YES)
- Collection of mobile adaptation related articles
- 小项目(servelt+jsp+mysql+EL+JSTL)完成一个登录功能的Servlet,具有增删改查的操作。实现登录身份验证,防止非法登录,防止多点登录,记住用户名密码功能。
猜你喜欢
Qt学习22 布局管理器(一)
[email protected]纳米颗粒)|纳米金属有机框架搭载雷帕霉素|科研试剂"/>
金属有机骨架材料ZIF-8包载姜黄素([email protected]纳米颗粒)|纳米金属有机框架搭载雷帕霉素|科研试剂
[Jilin University] information sharing of postgraduate entrance examination and re examination
JS input number and standard digit number are compared. The problem of adding 0 to 0
Qt学习25 布局管理器(四)
Uniapp tips - scrolling components
Common network state detection and analysis tools
jvm-对象生命周期
关于回溯问题中的排列问题的思考(LeetCode46题与47题)
JS first summary
随机推荐
Scroll detection, so that the content in the lower right corner is not displayed at the top of the page, but is displayed as the mouse slides
Page generation QR code
GoLand 2021.1: rename the go project
Doxorubicin loaded on metal organic framework MIL-88 DOX | folic acid modified uio-66-nh2 doxorubicin loaded [email
Using registered classes to realize specific type matching function template
可编程逻辑器件软件测试
JS matrix zero
JS input number and standard digit number are compared. The problem of adding 0 to 0
Thrift threadmanager and three monitors
Richview trvstyle liststyle list style (bullet number)
1px problem of mobile terminal
JS Part III
Too many files with unapproved license
How to use lxml to judge whether the website announcement is updated
Webpage connection database ~ simple implementation of addition, deletion, modification and query complete code
RocksDB LRUCache
Formation of mil-100 (FE) coated small molecule aspirin [email protected] (FE) | glycyrrhetinic acid modified metal organ
Onmenusharetimeline custom shared content is invalid, and the title and icon are not displayed
金属有机骨架MIL-88负载阿霉素DOX|叶酸修饰UiO-66-NH2负载阿霉素[email protected]纳米粒子
JVM runtime data area