Algorithm fibonacci级数的算法函数
我不是在寻找答案,但我在寻找这个问题的答案。在面试时发现这个问题,但不确定他们在问什么 编写遍历斐波那契序列并返回 作为参数传入的索引Algorithm fibonacci级数的算法函数,algorithm,fibonacci,Algorithm,Fibonacci,我不是在寻找答案,但我在寻找这个问题的答案。在面试时发现这个问题,但不确定他们在问什么 编写遍历斐波那契序列并返回 作为参数传入的索引 首先,你可以从维基上更新关于斐波那契的基本数学信息。看看快速计算。你可以在中阅读所有关于它的内容 这是计算第n个斐波那契数的递归函数,时间为O(2^n): 计算序列 你可能会说,在实际计算 在计算机上使用斐波那契序列,最好使用原始序列 递推关系,f[n]=f[n−1] +f[n−2]. 我倾向于同意。使用 对于大n的直接闭式解,需要保持 非常精确。即使小数点后有
首先,你可以从维基上更新关于斐波那契的基本数学信息。看看快速计算。你可以在中阅读所有关于它的内容 这是计算第n个斐波那契数的递归函数,时间为O(2^n): 计算序列 你可能会说,在实际计算 在计算机上使用斐波那契序列,最好使用原始序列 递推关系,f[n]=f[n−1] +f[n−2]. 我倾向于同意。使用 对于大n的直接闭式解,需要保持 非常精确。即使小数点后有9位, fn≈圆形(0.723606798⋅(1.618033989)n),例如,仅对 最多n=38(观察对比)。此外,添加整数也非常困难 计算成本较低,且比求指数更精确 符号分数或浮点值 这是计算第n个斐波那契数的更好方法,并且是O(n)时间: 这很容易理解,如果你把这个矩阵和向量相乘
f(n-1), f(n-2), ... , f(n-x+1), f(n-x)
导致
f(n), f(n-1), ... , f(n-x+1)
矩阵求幂可以在O(log(n))时间内完成(当x被认为是常数时)
对于斐波那契递推,还有一个闭合公式解,请参见此处,查找比奈公式或莫伊夫公式
看看:
1-我对这个问题的解释不同……给定一个
数字作为输入,该数字在序列中的索引是什么?e、 g.input=5
,则索引为5
(给定序列为0 1 2 3 5
,其中索引以0
开头)
代码如下所示(返回索引)[免责声明:根据中给出的代码改编]
在我看来,要求您返回第n个斐波那契编号,其中n是传递的参数。您可以使用各种方法来回答这个问题,而所有这些方法在时间复杂度和代码复杂度上都有所不同
方法1(使用递归)
一个简单的方法是直接递归实现上述数学递归关系
int fib(int n)
{
if ( n <= 1 )
return n;
return fib(n-1) + fib(n-2);
}
fib(2)fib(1)fib(1)fib(0)fib(1)fib(0)
/ \
fib(1)fib(0)
额外空间:O(n),如果我们考虑FucCUTE调用堆栈大小,否则O(1).
方法2(使用动态规划)
通过存储迄今为止计算的斐波那契数,我们可以避免方法1中重复的工作
int fib(int n)
{
/* Declare an array to store fibonacci numbers. */
int f[n+1];
int i;
/* 0th and 1st number of the series are 0 and 1*/
f[0] = 0;
f[1] = 1;
for (i = 2; i <= n; i++)
{
/* Add the previous 2 numbers in the series
and store it */
f[i] = f[i-1] + f[i-2];
}
return f[n];
}
时间复杂度:O(Logn)
额外空间:O(Logn)如果我们考虑函数调用堆栈大小,否则O(1).
驱动程序:
int main()
{
int n=9;
printf(“%d”,fib(9));
getchar();
返回0;
}
参考资料:
这是一个措辞非常拙劣的问题,但您必须假设他们询问的是第n个Fibonnaci编号,其中提供了n
作为参数
除了其他人列出的所有技术之外,对于n>1
,您还可以使用,这比任何迭代方法都要快。但是,正如问题所说的“遍历斐波那契序列”,这可能不符合条件。你可能还会把他们吓死。啊……这就更清楚了。你读过我的答案了吗?假设给出了索引i。a) run-thru fib series:1 1(它没有说明要走多远或做什么?)b)从您的答案中返回iMade一个咖啡脚本版本:
f(n), f(n-1), ... , f(n-x+1)
public static int fibonacci(int i){
if(i==0)
return 0;
if(i==1)
return 1;
return fib(--i,0,1);
}
public static int fib(int num,int pre,int prepre){
if(num==0){
return prepre+pre;
}
return fib(--num,pre+prepre,pre);
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
if ( n== 1 )
return 1;
int fib1 = 0;
int fib2 = 1;
int fib = 0;
int i = 0;
for (i = 2; ; i++ )
{
fib = fib1 + fib2;
if ( n == fib )
break;
fib1 = fib2;
fib2 = fib;
}
return i;
}
int fib(int n)
{
if ( n <= 1 )
return n;
return fib(n-1) + fib(n-2);
}
fib(5)
/ \
fib(4) fib(3)
/ \ / \
fib(3) fib(2) fib(2) fib(1)
/ \ / \ / \
int fib(int n)
{
/* Declare an array to store fibonacci numbers. */
int f[n+1];
int i;
/* 0th and 1st number of the series are 0 and 1*/
f[0] = 0;
f[1] = 1;
for (i = 2; i <= n; i++)
{
/* Add the previous 2 numbers in the series
and store it */
f[i] = f[i-1] + f[i-2];
}
return f[n];
}
int fib(int n)
{
int a = 0, b = 1, c, i;
if( n == 0)
return a;
for (i = 2; i <= n; i++)
{
c = a + b;
a = b;
b = c;
}
return b;
}
/* Helper function that multiplies 2 matricies F and M of size 2*2, and
puts the multiplication result back to F[][] */
void multiply(int F[2][2], int M[2][2]);
/* Helper function that calculates F[][] raise to the power n and puts the
result in F[][]
Note that this function is desinged only for fib() and won't work as general
power function */
void power(int F[2][2], int n);
int fib(int n)
{
int F[2][2] = {{1,1},{1,0}};
if(n == 0)
return 0;
power(F, n-1);
return F[0][0];
}
void multiply(int F[2][2], int M[2][2])
{
int x = F[0][0]*M[0][0] + F[0][1]*M[1][0];
int y = F[0][0]*M[0][1] + F[0][1]*M[1][1];
int z = F[1][0]*M[0][0] + F[1][1]*M[1][0];
int w = F[1][0]*M[0][1] + F[1][1]*M[1][1];
F[0][0] = x;
F[0][1] = y;
F[1][0] = z;
F[1][1] = w;
}
void power(int F[2][2], int n)
{
int i;
int M[2][2] = {{1,1},{1,0}};
// n - 1 times multiply the matrix to {{1,0},{0,1}}
for ( i = 2; i <= n; i++ )
multiply(F, M);
}
void multiply(int F[2][2], int M[2][2]);
void power(int F[2][2], int n);
/* function that returns nth Fibonacci number */
int fib(int n)
{
int F[2][2] = {{1,1},{1,0}};
if(n == 0)
return 0;
power(F, n-1);
return F[0][0];
}
/* Optimized version of power() in method 4 */
void power(int F[2][2], int n)
{
if( n == 0 || n == 1)
return;
int M[2][2] = {{1,1},{1,0}};
power(F, n/2);
multiply(F, F);
if( n%2 != 0 )
multiply(F, M);
}
void multiply(int F[2][2], int M[2][2])
{
int x = F[0][0]*M[0][0] + F[0][1]*M[1][0];
int y = F[0][0]*M[0][1] + F[0][1]*M[1][1];
int z = F[1][0]*M[0][0] + F[1][1]*M[1][0];
int w = F[1][0]*M[0][1] + F[1][1]*M[1][1];
F[0][0] = x;
F[0][1] = y;
F[1][0] = z;
F[1][1] = w;
}