1130: 母牛生小牛

版权声明:我的就是我的 https://blog.csdn.net/qq_41638851/article/details/89116673

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
int f(int N){
	
	if(N<4)
	return 1;
	else
	return f(N-1)+f(N-3);
}
int main(){
    int N;
	cin>>N;
	cout<<f(N);
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main() {
	int n;
	cin>>n;
	int a,b,c,d;   //分别代表一岁二岁三岁四岁及其以上的母牛的数目。
	if(n<4)cout<<1<<endl;
	else {
		n=n-4;
		a=1;
		b=c=0;
		d=1;
		for(int i=0; i<n; i++) {
			d=c+d;//当年大母牛数目d为前一年c和d的总和
			c=b;//b母牛长了一岁到了c
			b=a;//同上
			a=d;//长成的母牛和原本的母牛共生出新的小母牛a个
		}
		cout<<a+b+c+d;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_41638851/article/details/89116673
今日推荐