The story of Hdu 2018 cows __hdu

Source: Internet
Author: User
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;
}


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.