首页 > 其他 > 详细

codevs1842 递归第一次

时间:2017-06-22 18:06:12      阅读:298      评论:0      收藏:0      [点我收藏+]
题目描述 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

 

#include<iostream>
#include <algorithm>
using namespace std;
int f(int x){ if(x>=0) return 5; else return f(x+1)+f(x+2)+1; }
int main(){ int n; cin>>n; cout<<f(n); return 0; }

codevs1842 递归第一次

原文:http://www.cnblogs.com/codetogether/p/7066346.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!