Main topic:
There is a cow, which has a small cow at the beginning of each year. Each heifer starts its fourth year with a heifer. Enter positive integer n, output How many cows are there in the first n years?
Problem Solving Ideas:
This topic is very simple at first glance, but I made a fatal mistake when I was careless. I first calculated the cow's value when n=1,2,3,4, because only one cow was in production at this time, so the cow book f=1,2,3,4. However, when I continued to calculate the n=5, for the first time I had forgotten the cow that was born at the beginning of the year also to the age of production, forgot to add this cow, changed the mistake, in the calculation of the n=6 of the old cow's second cow at this time also to the age of production, this year equivalent to three cows in production. And so on, although the topic is not difficult, but very cumbersome. In my calculations, I vaguely sensed what the rules should be in these numbers, and then I calculated the number of the first 7 years, and when n=1,2,3,4,5,6,7 the number of cows at this time was f=1,2,3,4,6,9,13, it was not difficult to find, starting with n=5, F (n) =f (n-4 +f (n-3) +f (n-2), after knowing the law, the problem will be solved later. Define an array of length 55 cow[55], used to store the number of cows under the index of the array, according to the number of rules cow[5] to cow[54], according to the output of the N value, directly from the array of n as subscript, which should be considered as a memory search bar.
Feelings:
In fact, when we do the problem, sometimes it is difficult to understand the problem at the end of the question how to do, this time we must first understand the topic of the test instructions, and then you can put some of the calculation results listed first, find out what the law, do not be impatient.
The code is as follows:
#include <iostream>
using namespace Std;
int cow[55];
int main ()
{
cow[1]=1,cow[2]=2,cow[3]=3,cow[4]=4;
int n;
for (int i=5;i<55;i++)
COW[I]=COW[I-4]+COW[I-3]+COW[I-2];
while (Cin>>n)
if (n!=0)
cout<<cow[n]<<endl;
System ("pause");
return 0;
}
Thematic three Problem1013