编程求解母牛问题。若一头母牛,从出生起第四个年头开始每年生一头母牛,按此规律,第 n年时有多少头母牛?
//=================================== //EX0209.cpp //母牛问题 //=================================== #include<iostream> using namespace std; //----------------------------------- int main() { int c=1,n; cin>>n; for(int i=4,a=1,b=1,tmp; i<=n; i++) { tmp = a+c; a=b; b=c; c=tmp; } cout<<c<<"\n"; }//==================================
#include <iostream> (720)#include <cmath> #include <vector> using namespace std; int f(int n); int main() { int years(10); cout << f(years); } int f(int n) { // wtf if (n == 1 || n == 2 || n == 3) return 1; if (n > 3) return (f(n - 1) + f(n - 3)); }
这道题你会答吗?花几分钟告诉大家答案吧!
扫描二维码,关注牛客网
下载牛客APP,随时随地刷题