Problem M (cow production problem)

Source: Internet
Author: User

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)

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.