liushuo

 

斐波那契数列

其一:
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);
  }
 }
}

posted on 2009-04-19 09:54 刘硕 阅读(206) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿(2)

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜