Core Tip: There used to be a pair of long-lived sons, they gave birth to a pair of sons every month, and the new little sons grew up in two months.
Once upon a time, there was a pair of long-lived sons. They gave birth to a pair of sons every month. The newly born little sons grew up in two months. At the end of the second month, their next generation sons began to be born. To give birth, solve the sequence of the increasing number of children.
*Problem analysis and algorithm design
The problem can be abstracted into the following mathematical formula:
Un=Un-1+Un-2
in:
n is the number of items (n>=3). It is the famous Fibonacci sequence, the first few of the sequence are: 1, 1, 2, 3, 5, 8, 13, 21...
The Fibonacci sequence can be processed in a variety of ways in a program. According to its general term recursion formula, the requirements of the question can be realized by using the most basic loop control.
*Program description and notes
#include
intmain()
{
int n,i,un1,un2,un;
for(n=2;n<3;)
{
printf("Please enter required number of generation:");
scanf("%d",&n);
if(n<3) printf("\n Enter error!\n"); /*Control input the correct N value*/
}
un = un2 = 1;
printf("The repid increase of rabbits in first %d generation is as felow:\n",n);
printf("l\tl\t");
for(i=3;i<=n;i++)
{
un1=un2;
un2 = un;
un=un1+un2; /*Use the general term formula to solve the value of N term*/
printf(i%10?"%d\t":"%d\n",un);
}
printf("\n");
}
*operation result
Please enter required number of generation: 20
The repid increase of rabbits in first 20 generation is as felow:
1 1 2 3 5 8 13 21 34 55
89 144 233 377 610 987 1597 2584 4181 6765
对我有用(4)对我没用(0)
我要报错我要收藏