现在的位置: 首页 > 综合 > 正文

fibonacci数列的两种实现方式

2013年07月08日 ⁄ 综合 ⁄ 共 454字 ⁄ 字号 评论关闭
package com.anllin.test;

public class Fibonacci
{
public static void main(String[] args)
{
System.out.println(fibonacciWithRecursion(
30));
System.out.println(fibonacciWithNotRecursion(
30));
}

public static long fibonacciWithRecursion(int pos)
{
if (1 == pos || 2 == pos)
return 1;
else
return fibonacciWithRecursion(pos - 1) + fibonacciWithRecursion(pos - 2);
}

public static long fibonacciWithNotRecursion(int pos)
{
long f = 0L;
long f1 = 1;
long f2 = 1;

for(int i= 0; i<pos-2;i++)
{
f
= f1 + f2;
f1
= f2;
f2
= f;
}

return f;
}
}

  

抱歉!评论已关闭.