Problem:
This problem is mainly a recursive algorithm, examining to be careful, the heifer is since it was born in the beginning of the fourth year of birth, the recursive formula for N=a (n-1) + (n-3).
Before looking at someone else's algorithm with a large number, in fact, not necessary, in the range of int can be installed.
The story of a cowTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 51574 Accepted Submission (s): 25849
Problem description has a cow, which has a heifer at the beginning of every year. Each heifer starts its fourth year with a heifer. Please program implementation how many cows are there in the first n years?
Input data consists of multiple test instances, one for each test instance, including an integer n (0<n<55), and the meaning of n as described in the topic.
N=0 indicates the end of the input data and does not handle it.
Output for each test instance, outputs the number of cows in the nth year.
Each output occupies one row.
Sample Input
2450
Sample Output
246
Code:
Import java.util.*;p ublic class main{public static void Main (String args[]) {Scanner cin=new Scanner (system.in); while ( Cin.hasnext ()) {int n=cin.nextint (); if (n==0) break;else{if (n<=4) {int sum=0;sum=n; SYSTEM.OUT.PRINTLN (sum);} Else{int a[]=new int[55];int sum=4;a[1]=a[2]=a[3]=a[4]=1;for (int i=5;i<=n;i++) {a[i]=a[i-1]+a[i-3];sum+=a[i];} SYSTEM.OUT.PRINTLN (sum);}}}}
Hdu 2018 Cow Story (Java)