标签:dev space sample 题解 panel std 描述 tput output
同学们在做题时常遇到这种函数
f(x)=5 (x>=0)
f(x)=f(x+1)+f(x+2)+1 (x<0)
下面就以这个函数为题做一个递归程序吧
一个数表示f(x)中x值
大家注意就一个数,前面代表样例编号
一个数表示值
大家注意就一个数,前面代表样例编号
样例一:0
样例二:-5
样例一:5
样例二:77
x>=-30
递归
#include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> using namespace std; int x,ans; int read() { int x=0,f=1; char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1; ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘; ch=getchar();} return x*f; } int f(int x) { if(x>=0) return 5; return f(x+1)+f(x+2)+1; } int main() { x=read(); ans=f(x); printf("%d",ans); return 0; }
标签:dev space sample 题解 panel std 描述 tput output
原文地址:http://www.cnblogs.com/z360/p/7503195.html