The story of the cow
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 44955 accepted Submission (s): 22309
Problem Description has a cow, which has a heifer at the beginning of every year. Each heifer starts in the fourth year and also has a heifer at the beginning of each of the first few years. Please program to realize how many cows there are in the first n years.
Input input data consists of multiple test instances, one row for each test instance, including an integer n (0<n<55) and the meaning of n as described in the topic.
N=0 represents the end of the input data and does not process it.
Output for each test instance, the number of cows in the nth year.
Each output occupies one row.
Sample Input
2 4 5 0
Sample Output
2 4 6
According to the derivation: the number of cows in the nth year equals the number of cows in the previous year plus the number of cows of the first n-3 years (the heifer was born before) n = n-1 + n-3
#include <iostream>
using namespace Std;
int fun (int m)
{
if (m>0&&m<5)
return m;
Else
Return Fun (m-1) +fun (m-3);
}
int main ()
{
int n;
while (Cin>>n&&n)
Cout<<fun (n) <<endl;
return 0;
}