[HDU] [2018] The Story of the Cow

Problem Description

Loading...

Problem Analysis

The idea is to simulate with a loop
going through one year at a time

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
int main() {
	int i,n,a,b,c,d;
	while(scanf("%d",&n),n!=0) {
		if(n<=4)
			printf("%d\n",n);
		else {
			n=n-4;
			a=b=c=d=1;
			for(i=0; i<n; i++) {
				a=a+b;
				b=c;
				c=d;
				d=a;
			}
			printf("%d\n",a+b+c+d);
		}
	}
	return 0;
}
This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...