A. Title Description
Have a cow, it is born heifer beginning of each year. Each heifers from the fourth year, but also the beginning of a heifer born each year. Please programming in year n, when the total number of cows?
Entry
A plurality of test input data examples, each test case per line, comprising an integer n (0 <n <55) , n the meanings as described in the title.
n = 0 indicates the end of input data, without processing.
Export
For each test case, the number of outputs of the n-th time in cows.
Each output per line.
Sample input
2
4
5
0
Sample Output
2
4
6
II. Solving ideas
The cows divided into four 1-year-old cow, the cow 2 years old, 3-year-old heifers, mature cows
With each passing year
The number of adult cows for 2-year-old cow of the year, the number of adult cows +
Number 3-year-old cow was 2 years the number of cows
Number 2-year-old cow was 1 year old cow number
Number 1-year-old cow is the number 1 adult cow
The total amount is the sum of the number of all cows
III. Sample Code
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext() ) {
int n = scanner.nextInt();
if(n == 0) return ;
int num1 = 0; //1岁母牛
int num2 = 0; //2岁母牛
int num3 = 0; //3岁母牛
int num4 = 1; //成年母牛
for(int i=1;i<n;i++){
num4 = num3+num4;
num3 = num2;
num2 = num1;
num1 = num4;
}
System.out.println(num1+num2+num3+num4);
}
}
}