Topic: There is 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?
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.
For each test case, the number of outputs of the n-th time in cows.
Each output per line.
2 4 5 0
2 . 4 . 6
Relationship between the number of cows and year in the following table
years | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
Quantity | 1 | 2 | 3 | 4 | 6 | 9 | 13 |
In the fifth year since the second generation of a small cow began to produce so there are six cows, six years to the first year of production of small cow in a cow, the third year of two small cows produce two small cows so a total of 9 cows, so the table can be seen that the number of cows from the beginning of the first year started regularly, F (n) = F (n-1) + F (n-3), i.e., the number of cows is then the number of cows the previous year plus the number of cows the previous three years.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n;
while((n=in.nextInt())!=0){
if(n>=1&&n<=4)
System.out.println(n);
else if(n>4){
System.out.println(Fun(n));
}
}
}
public static int Fun(int m){
if(m==1)
return 1;
else if(m==2)
return 2;
else if(m==3)
return 3;
else if(m==4)
return 4;
else
return Fun(m-1)+Fun(m-3);
}
}