斐波那契(Fibonacci)数列的第1和第2个数分别为1和1,从第3个数开始,每个数等于前两个数之和(1,1,2,3,5

2025-03-18 18:03:51
推荐回答(2个)
回答1:

1、 1 2 、1 3 、2 4 、3 5 、5 6 、8 7 、13 8 、21 9 、34 10、 55 11 、89 12 、144 13 、233 14 、377 15 、610 16 、987 17 、1597 18 、2584 19 、4181 20 、6765 。我不行了,够了吧???
对于斐波那契数列1、1、2、3、5、8、13、…….有如下定义 F(n)=f(n-1)+f(n-2) F(1)=1 F(2)=1 对于以下矩阵乘法 F(n+1) = 1 1 * F(n) F(n) 1 0 F(n-1) 它的运算就是 F(n+1)=F(n)+F(n-1) F(n)=F(n) 可见该矩阵的乘法完全符合斐波那契数列的定义 设1 为B,1 1为C 1 1 0 可以用迭代得到: 斐波那契数列的某一项F(n)=(BC^(n-2))1 这就是斐波那契数列的矩阵乘法定义。 另矩阵乘法的一个运算法则A¬^n(n为偶数)=A^(n/2)* A^(n/2). 因此可以用递归的方法求得答案。 时间效率:O(logn),比模拟法O(n)远远高效。 代码(PASCAL) {变量matrix是二阶方阵,matrix是矩阵的英文} program fibonacci; type matrix=array[1..2,1..2] of qword; var c,cc:matrix; n:integer; function multiply(x,y:matrix):matrix; var temp:matrix; begin temp[1,1]:=x[1,1]*y[1,1]+x[1,2]*y[2,1]; temp[1,2]:=x[1,1]*y[1,2]+x[1,2]*y[2,2]; temp[2,1]:=x[2,1]*y[1,1]+x[2,2]*y[2,1]; temp[2,2]:=x[2,1]*y[1,2]+x[2,2]*y[2,2]; exit(temp); end; function getcc(n:integer):matrix; var temp:matrix; t:integer; begin if n=1 then exit(c); t:=n div 2; temp:=getcc(t); temp:=multiply(temp,temp); if odd(n) then exit(multiply(temp,c)) else exit(temp); end; procedure init; begin readln(n); c[1,1]:=1; c[1,2]:=1; c[2,1]:=1; c[2,2]:=0; if n=1 then begin writeln(1); halt; end; if n=2 then begin writeln(1); halt; end; cc:=getcc(n-2); end; procedure work; begin writeln(cc[1,1]+cc[1,2]); end; begin init; work; end.
希望采纳。。。。。。。。我很辛苦。。。

回答2:

本打算叫你自己想的...最后还是忍不住 写出来了..没有EC也没发测试..你去测一下
. 我觉得..没有错吧...老早以前做过的题..呵呵
每行输出5个的...就不用我写了吧....那个简单....
int a = 1;
int b = 0;
int c = 0;
for (int i=0 ; i <50 ; i++)
{
c = a + b ;
a = b;
b = c ;
输出c;
}
哈..你测试一下哦!