Thematic three Problem1013

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.