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. <br>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. <br> each output takes up one row.
Sample Input2450 Sample Output246 ideas: Almost every four years a cycle, the first three years F (1) = 1,f (2) = 2,f (3) = 3,f (4) = f (3) + 1,f (6) = f (5) + 1....F (n) = F (n-1) + 1; F (4) = f (3) + F (1), one cycle every four years, can be alternately circulated to solve the code:
#include <iostream>
using namespace Std;
int main ()
{
int n = 0;
int day = 0;
int f1,f2,f3,f4;
while (Cin >> Day && Day! = 0) {
F1 = 1;
F2 = 2;
F3 = 3;
if (day = = 0) return 0;
if (Day < 4) F4 = day;
if (Day >= 4)
for (int i = 4;i <= day;i++) {
F4 = F1 + F3;
F1 = F2;
F2 = F3;
F3 = F4;
}
cout << f4 << Endl;
}
return 0;
}
Problem M (cow production problem)