当前位置:网站首页>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;
} 
边栏推荐
- Nucleic acid modified metal organic framework drug carrier | pcn-223 metal organic framework encapsulated ad adamantane | zif-8 encapsulated adriamycin (DOX)
- 全局事件总线
- Metal organic framework material zif-8 containing curcumin( [email protected] Nanoparticles) | nano metal organic framework carry
- Uniapp skills - scrolling components -1
- Qt学习23 布局管理器(二)
- Uniapp tips - set background music
- Common network state detection and analysis tools
- Print. JS -- web page file printing
- 28: Chapter 3: develop Passport Service: 11: define attributes in the configuration file, and then obtain them in the code;
- Richview trvstyle liststyle list style (bullet number)
猜你喜欢

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

Common network state detection and analysis tools

Installation impression notes

JS Part 2

QT learning 20 standard dialog box in QT (middle)
[email protected] Nanoparticles) | nano metal organic framework carry"/>Metal organic framework material zif-8 containing curcumin( [email protected] Nanoparticles) | nano metal organic framework carry

QT learning 25 layout manager (4)

Comprehensive case of MySQL data addition, deletion, modification and query

交联环糊精金属有机骨架负载甲氨蝶呤缓释微粒|金属-有机多孔材料UiO-66负载黄酮苷类药物|齐岳

Qt学习17 对话框及其类型
随机推荐
Redis: redis data structure and key operation commands
Conversion function and explicit
如何使用lxml判断网站公告是否更新
Uniapp tips - scrolling components
JVM runtime data area
Why are grass-roots colleges and universities with "soil and poverty" called "Northeast small Tsinghua"?
Go: send the get request and parse the return JSON (go1.16.4)
GoLand 2021.1: rename the go project
[ACNOI2022]猜数
Metal organic framework (MOFs) antitumor drug carrier | pcn-223 loaded with metronidazole | uio-66 loaded with ciprofloxacin hydrochloride(
JS new challenges
核酸修饰的金属有机框架药物载体|PCN-223金属有机骨架包载Ad金刚烷|ZIF-8包裹阿霉素(DOX)
Go language web development series 28: solve cross domain access of CORS with gin contrib / CORS
GoLand 2021.2 configure go (go1.17.6)
Solve the problem of dormitory router campus network sharing login
Function calling convention
[Jilin University] information sharing of postgraduate entrance examination and re examination
Invalid Z-index problem
Spring cup eight school league
Collection of mobile adaptation related articles