course(递归专题)求累加和
This commit is contained in:
parent
e862efa6c7
commit
19ac0b6144
|
@ -0,0 +1,3 @@
|
|||
# 标题
|
||||
|
||||
* 内容
|
|
@ -0,0 +1,18 @@
|
|||
# 『入门』(递归专题)求累加和
|
||||
[问题描述]
|
||||
|
||||
教学案例,必须使用递归函数完成求解!
|
||||
|
||||
编程求解下列式子的值:s=1+2+3+...+n。
|
||||
|
||||
[输入格式]
|
||||
一行,只有一个整数n。(1 <= n <= 1000)
|
||||
|
||||
[输出格式]
|
||||
1个整数,即s的值。
|
||||
|
||||
[输入样例]
|
||||
100
|
||||
|
||||
[输出样例]
|
||||
5050
|
|
@ -0,0 +1,15 @@
|
|||
//½×¼Ó£¨³¬Ð¡£©
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
long long dg(long long n){
|
||||
if(n==1){
|
||||
return 1;
|
||||
}
|
||||
return n+dg(n-1);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
cin>>n;
|
||||
cout<<dg(n);
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1 @@
|
|||
1
|
|
@ -0,0 +1 @@
|
|||
100
|
|
@ -0,0 +1 @@
|
|||
1000
|
|
@ -0,0 +1,18 @@
|
|||
## z:\Chao\src\1660_n+\test\in.txt
|
||||
2020/04/24 周五 13:47:20.56
|
||||
1
|
||||
-----------------------------------------------
|
||||
Process exited after 160 ms with return value 0
|
||||
|
||||
## z:\Chao\src\1660_n+\test\in2.txt
|
||||
2020/04/24 周五 13:47:20.56
|
||||
5050
|
||||
-----------------------------------------------
|
||||
Process exited after 80 ms with return value 0
|
||||
|
||||
## z:\Chao\src\1660_n+\test\in3.txt
|
||||
2020/04/24 周五 13:47:20.56
|
||||
500500
|
||||
-----------------------------------------------
|
||||
Process exited after 80 ms with return value 0
|
||||
|
Loading…
Reference in New Issue