Describe:
There is a cow, which has a small cow at the beginning of each year. Each heifer starts its fourth year with a heifer. Please program implementation how many cows are there in the first n years? Input: The 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, output the number of cows in the nth year.
Each output occupies one row. input:2450output:246 Analysis: The difficulty in each calf in the fourth year also produces a calf, we can list 1-8 years of growth law to draw a recursive sum[n]=sum[n-1]+sum[n-3] (n>=5), We can understand this recursion, from the 5th year onwards, the second year of the production of calves also began to produce, that is, the second year all the cattle in the 5th year will be produced, the fourth year of the existence of the cattle five years are in, but not all will produce, so sum[5]=sum[4]+sum[3]. In this way, the total number of cattle in the nth year is equal to the total number of n-1 years plus the total number of cattle n-3 four years ago.
1#include <iostream>2 using namespacestd;3 4 intMain ()5 {6 Static inta[ -];7a[1] =1; a[2] =2; a[3] =3; a[4] =4;8 for(inti =5; I < -; i++)9A[i] = a[i-1] + a[i-3];Ten ints; One while(Cin >> s) && (s! =0)) Acout << A[s] <<Endl; -System"Pause"); - return 0; the}
2018-The story of the cow