- public class ha {
- ha() throws Exception
- {
- System.out.println("費氏數列第12項: "+f(12));
- System.out.println("費氏數列第23項: "+f(23));
- System.out.println("費氏數列第37項: "+f(37));
- System.out.println("費氏數列第42項: "+f(42));
- }
- int f(int n)
- {
- if(n<2)
- {
- return n;
- }
- else
- {
- return f(n-2)+f(n-1);
- }
- }
- public static void main(String[] args) throws Exception {
- long start=System.currentTimeMillis();
- new ha();
- long end=System.currentTimeMillis();
- System.out.println("花費: "+(end-start)+" 毫秒");
- }
- }
複製代碼- public class ha {
- ha() throws Exception
- {
- long data[]=new long[90];
- data[0]=0;
- data[1]=1;
- for(int i=2; i<90; i++)
- {
- data[i]=data[i-2]+data[i-1];
- }
- System.out.println("費氏數列第12項: "+data[12]);
- System.out.println("費氏數列第23項: "+data[23]);
- System.out.println("費氏數列第37項: "+data[37]);
- System.out.println("費氏數列第42項: "+data[42]);
- System.out.println("費氏數列第59項: "+data[59]);
- System.out.println("費氏數列第89項: "+data[89]);
- }
- public static void main(String[] args) throws Exception {
- long start=System.currentTimeMillis();
- new ha();
- long end=System.currentTimeMillis();
- System.out.println("花費: "+(end-start)+" 毫秒");
- }
- }
複製代碼 |