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);
}