2018-The story of the cow

Source: Internet
Author: User

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

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.