其一:
public class Fib1{
public static void main(String []args){
System.out.println(f(7));
}
public static long f(int index){
if(index == 1 || index == 2){
return 1;
}
long f1= 1L;
long f2 = 1L;
long f = 0;
for(int i=0;i<index-2;i++){
f = f1+f2;
f1 = f2;
f2 = f;
}
return f;
}
}
其二:
//package demo;
//打印斐波那契数列
public class Fib{
public static void main(String[] args){
int x = 0;
int y = 1;
System.out.print(x+" ");
for(int i=1;i<=20;i++){
System.out.print(y+" ");
y=x+y;
x=y-x;
}
}
}
其三:
//求出第n个数是几
public class Fib{
public static void main(String args[]){
System.out.println(f(6));
}
public static int f(int n){
if(n == 1 || n == 2){
return 1;
}else{
return f(n-1) + f(n-2);
}
}
}