The problem of mouse reproduction

Source: Internet
Author: User
Tags printf
Problem:
A pair of mice each month to give birth to a small mouse, small mice grow 3 months, starting from the 4th month can also be each

The moon gave birth to a pair of small rats. If all the rats were not dead, how many mice were there after 2 years (24 months).


Business logic: The mouse is divided into old (born more than 3 months), first (born one months), second (born two months), third (born 3 months)



The code is as follows:

#include <stdio.h>
void Main ()
{
int old = 2,first = 0,second = 0,third = 0;
int i = 0;
for (i = 0; i <; i++)
{
Old = old + third;
Third = second;
second = first;
first = old;
}
printf ("%d\n", Old+first+second+third);
}



N years after the number of components: 2 years after the total mouse = The first two mice + the two mice 2 years symbiotic how many + annual newborn mice after 2 years of birth


The code is as follows:

#include <stdio.h>
#define MONTH 24
void Main ()
{
int i,sum=0,mouse[month]={1,1,1,1}; Mouse[i] Indicates how many pairs of newborn mice I have
for (i=1;i<=month-3;i++)//month of newborn mice after the total number of births
{
if (i>=4)
{
MOUSE[I]=MOUSE[I-1]+MOUSE[I-3];
}
sum+=mouse[i]* (month-i-2);
printf ("%6d month freshman%6d Yes, they will have%6d on each pair in the planned two years." \ n ", i,mouse[i],month-i-2);
}
Sum=sum+mouse[0]+month; Plus the first two mice and the two mice, a symbiotic rat.
printf ("\n\t\t%d months later there were%6d mice." \ n ", month,sum*2);

}



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.