斐波拉契

 1 public class Fibonacci {
 2 
 3         public static void main(String[] args) {
 4                 System.out.println(inti(3));
 5         }
 6        
 7         private static long inti(long n) {
 8                 if (n <= 1) {
 9                         return n;
10                 }
11                 long firstLong = 0;
12                 long secondLong = 1;
13                 for (int i = 0; i < n-1; i++) {
14                         long sum = firstLong + secondLong;
15                         firstLong = secondLong;
16                         secondLong = sum;
17                 }
18                 return secondLong;
19         }
20 
21 }

posted @ 2020-10-02 13:52  wolf0000  阅读(125)  评论(0)    收藏  举报