当前位置:网站首页>Exercise 10-1 calculate the sum of 1 to n using recursive functions
Exercise 10-1 calculate the sum of 1 to n using recursive functions
2022-07-03 14:09:00 【ᯤ⁹ᴳ⁺ ·】
practice 10-1 Use recursive functions to calculate 1 To n The sum of the (10 branch )
This problem requires the realization of a recursive calculation 1+2+3+…+n Simple function of sum of .
Function interface definition :
int sum( int n );
This function is used for the positive integer passed in n
return 1+2+3+…+n
And ; if n
If it is not a positive integer, return 0. The title guarantees that the input and output are in the long integer range . It is suggested to try to write recursive functions .
Sample referee test procedure :
#include <stdio.h>
int sum( int n );
int main()
{
int n;
scanf("%d", &n);
printf ("%d\n", sum(n));
return 0;
}
/* Your code will be embedded here */
sample input 1:
10
sample output 1:
55
sample input 2:
0
sample output 2:
0
int sum( int n ){
int s;
if(n<=0){
s=0;
}else{
s=n+sum(n-1);
}
return s;
}
边栏推荐
- Configure stylelint
- Article content typesetting and code highlighting
- 消息订阅与发布
- Doxorubicin loaded on metal organic framework MIL-88 DOX | folic acid modified uio-66-nh2 doxorubicin loaded [email
- Use and design of Muduo buffer class
- Implementation of Muduo asynchronous logging
- JVM class loading
- Uniapp tips - scrolling components
- Go 1.16.4: purpose of go mod tidy
- Current situation, analysis and prediction of information and innovation industry
猜你喜欢
Solution to failure or slow downloading of electron when electron uses electron builder to package
Message subscription and publishing
Fabric. JS document
How to use lxml to judge whether the website announcement is updated
JS Part 2
八大排序
Metal organic framework MOFs loaded with non steroidal anti-inflammatory drugs | zif-8 wrapped Prussian blue loaded quercetin (preparation method)
MySQL 数据增删改查综合案例
28:第三章:开发通行证服务:11:在配置文件中定义属性,然后在代码中去获取;
GoLand 2021.1.1: configure the multi line display of the tab of the open file
随机推荐
JS Part 2
Spring cup eight school league
Redis: commandes d'action pour les données de type chaîne
Toast UI editor (editor allows you to edit your markup document using text or WYSIWYG, with syntax highlighting, scrolling synchronization, real-time preview and chart functions.)
The small project (servlet+jsp+mysql+el+jstl) completes a servlet with login function, with the operation of adding, deleting, modifying and querying. Realize login authentication, prevent illegal log
Scroll detection of the navigation bar enables the navigation bar to slide and fix with no content
concat和concat_ws()区别及group_concat()和repeat()函数的使用
[acnoi2022] guess numbers
Onmenusharetimeline custom shared content is invalid, and the title and icon are not displayed
Failure of vector insertion element iterator in STL
Solve the problem of dormitory router campus network sharing login
Vite project commissioning
Interface for querying IP home
Qt学习22 布局管理器(一)
Doxorubicin loaded on metal organic framework MIL-88 DOX | folic acid modified uio-66-nh2 doxorubicin loaded [email
金属有机骨架材料ZIF-8包载姜黄素([email protected]纳米颗粒)|纳米金属有机框架搭载雷帕霉素|科研试剂
JS new challenges
Metal organic framework material zif-8 containing curcumin( [email protected] Nanoparticles) | nano metal organic framework carry
QT learning 17 dialog box and its types
Comprehensive case of MySQL data addition, deletion, modification and query