用递归方法求解母牛问题。若一头母牛,从出生起第四个年头开始每年生一头母牛,按此规律,第n年时有多少头母牛?
//----------------------------------- //EX0504.cpp //母牛问题 //----------------------------------- #include<iostream> using namespace std; //----------------------------------- int f(int n){ if(n<=3) return 1; return f(n-1)+f(n-3); }//---------------------------------- int main() { int n; cin>>n; cout<<f(n)<<"\n"; }//----------------------------------
这道题你会答吗?花几分钟告诉大家答案吧!
扫描二维码,关注牛客网
下载牛客APP,随时随地刷题
//----------------------------------- //EX0504.cpp //母牛问题 //----------------------------------- #include<iostream> using namespace std; //----------------------------------- int f(int n){ if(n<=3) return 1; return f(n-1)+f(n-3); }//---------------------------------- int main() { int n; cin>>n; cout<<f(n)<<"\n"; }//----------------------------------