- public class Ch01 {
- Ch01()
- {
- 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) {
- long start=System.currentTimeMillis();
- new Ch01();
- long end=System.currentTimeMillis();
- System.out.println("花費: "+(end-start)+" 毫秒");
- }
- }
複製代碼 |