1842 递归第一次

时间:2022-05-07
本文章向大家介绍1842 递归第一次,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1842 递归第一次

题目描述 Description

同学们在做题时常遇到这种函数

f(x)=5 (x>=0)

f(x)=f(x+1)+f(x+2)+1 (x<0)

下面就以这个函数为题做一个递归程序吧

输入描述 Input Description

一个数表示f(x)中x值

大家注意就一个数,前面代表样例编号

输出描述 Output Description

一个数表示值

大家注意就一个数,前面代表样例编号

样例输入 Sample Input

样例一:0

样例二:-5

样例输出 Sample Output

样例一:5

样例二:77

数据范围及提示 Data Size & Hint

x>=-30

 1 #include<iostream>
 2 using namespace std;
 3 int f(int n)
 4 {
 5     if(n>=0)return 5;
 6     else return f(n+1)+f(n+2)+1;
 7 }
 8 int main()
 9 {
10     int n;
11     cin>>n;
12     cout<<f(n);
13     return 0;
14 }