Home | Web Board | ProblemSet | Standing | Status | Statistics |
Problem A: 兔子的繁殖问题
Time Limit: 1 Sec Memory Limit: 2 MBSubmit: 5993 Solved: 3700
[ Submit][ Status][ Web Board]
Description
假设一对兔子每月能生一对小兔(一雌一雄),每对小兔出生后的下一个月是没有繁殖能力的,至出生后的第三个月开始又可以每月生一队小兔,问从一对刚出生的小兔开始,经过若干个月后一共有多少兔子(假设在此过程中兔子没有死亡)?
这个问题是意大利数学家菲波那契(Fibonacci)在他1202年出版的《算盘全书》中提出来的,从第一对刚出生的小兔开始每月的兔子数被乘坐菲波那契序列。
Input
输入的第一个数为n,接下来有n个数字。每个数字为一个月份m(m<=45)。
Output
输出为n行,每行为第m个月后的兔子总数。
Sample Input
61 2 3 4 5 10
Sample Output
1235889
HINT
超时了吧!
使用递归计算菲波那契序列,这合适吗?
用数组写比较方便,用循环直接算也不超时。
Append Code
#include <stdio.h>
#include <stdlib.h>
int main() {
int a,b,m,i,n,n1,j;
scanf("%d",&n1);
for(i=1; i<=n1; i++) {
scanf("%d",&n);
n=n+1;
a=0;
b=1;
if(n==1) {
printf("%d\n",b);
} else {
for(j=2; j<=n; j++) {
m=a+b;
a=b;
b=m;
}
printf("%d\n",m);
}
}
return 0;
}