package com;
public class Fibonacci {
//非递归算法的斐波那契数列
public static int F1(int n){
if(n==0) return 0;
if(n==1) return 1;
int fn = 0;
int fn0 = 0;
int fn1 = 1;
for(int i=2; i<=n; i++)
{
fn = fn0 + fn1;
fn0 = fn1;
fn1 = fn;
}
return fn;
}
//递归算法的斐波那契数列
public static int F(int n){
if(n==0) return 0;
if(n==1 || n==2) return 1;
return F(n-1) + F(n-2);
}
public static void main(String[] args){
for(int i=0; i<10; i++){
System.out.println(F1(i));
}
for(int j=0; j<10; j++){
System.out.println(F(j));
}
}
}
斐波那契数列的Java实现
最新推荐文章于 2022-07-25 16:44:44 发布