The Cow's Tale (Recursive)

cow story

Time Limit: 1s Memory Limit: 128MB Committed: 131134 Solved: 41320

topic description

There was a cow who gave birth to a heifer at the beginning of each year. Each heifer starts from the fourth year and also gives birth to a heifer at the beginning of each year. Please program to realize how many cows there are in the nth year?

input format

The input data consists of multiple test cases, each test case occupies one line, including an integer n (0<n<55), the meaning of n is as described in the title.
n=0 means the end of the input data, no processing.

output format

For each test instance, output the number of cows in year n.
Each output occupies one line.

sample input

2
4
5
0

sample output

2
4
6
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+7;
long long n,a[100];
int main() {
	while(scanf("%lld",&n)){
		if(!n) break;
		if(n<=4)
			printf("%lld\n",n);
		else{
			long long ans=4,temp=2;
			for(int i=1;i<=n;i++){
				if(i<=4){
					a[i]=i;
				}else{
					a[i]=a[i-1]+a[i-3];
				}
			}
			printf("%lld\n",a[n]);
		}
	}
	return 0;
}

Supongo que te gusta

Origin blog.csdn.net/m0_56501550/article/details/130017236
Recomendado
Clasificación