返回列表 發帖
  1. public class ha {

  2.         ha() throws Exception
  3.         {
  4.                 System.out.println("費氏數列第12項: "+f(12));
  5.                 System.out.println("費氏數列第23項: "+f(23));
  6.                 System.out.println("費氏數列第37項: "+f(37));
  7.                 System.out.println("費氏數列第42項: "+f(42));
  8.         }
  9.         int f(int n)
  10.         {
  11.                 if(n<2)
  12.                 {
  13.                         return n;
  14.                 }
  15.                 else
  16.                 {
  17.                         return f(n-2)+f(n-1);
  18.                 }
  19.         }
  20.         public static void main(String[] args) throws Exception {               
  21.                 long start=System.currentTimeMillis();
  22.                 new ha();
  23.                 long end=System.currentTimeMillis();
  24.                 System.out.println("花費: "+(end-start)+" 毫秒");
  25.         }
  26. }
複製代碼
  1. public class ha {

  2.         ha() throws Exception
  3.         {
  4.                 long data[]=new long[90];
  5.                 data[0]=0;
  6.                 data[1]=1;
  7.                 for(int i=2; i<90; i++)
  8.                 {
  9.                         data[i]=data[i-2]+data[i-1];
  10.                 }
  11.                 System.out.println("費氏數列第12項: "+data[12]);
  12.                 System.out.println("費氏數列第23項: "+data[23]);
  13.                 System.out.println("費氏數列第37項: "+data[37]);
  14.                 System.out.println("費氏數列第42項: "+data[42]);
  15.                 System.out.println("費氏數列第59項: "+data[59]);
  16.                 System.out.println("費氏數列第89項: "+data[89]);
  17.         }

  18.         public static void main(String[] args) throws Exception {               
  19.                 long start=System.currentTimeMillis();
  20.                 new ha();
  21.                 long end=System.currentTimeMillis();
  22.                 System.out.println("花費: "+(end-start)+" 毫秒");
  23.         }
  24. }
複製代碼

TOP

返回列表