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 劉碩 閱讀(222) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 青冈县| 西城区| 沾化县| 正镶白旗| 雷山县| 西和县| 伊宁市| 平遥县| 吉林省| 大庆市| 永清县| 砚山县| 广丰县| 正定县| 罗城| 长葛市| 涿鹿县| 德格县| 绥中县| 商水县| 深水埗区| 峡江县| 略阳县| 古交市| 新田县| 类乌齐县| 公主岭市| 安岳县| 三原县| 贺兰县| 临猗县| 双辽市| 林甸县| 辽阳市| 方正县| 浑源县| 社会| 汽车| 德江县| 佛山市| 晋城|