首页 > 代码库 > zoj Fibonacci Numbers ( java , 简单 ,大数)

zoj Fibonacci Numbers ( java , 简单 ,大数)

题目

 

//f(1) = 1, f(2) = 1, f(n > 2) = f(n - 1) + f(n - 2)

 

import java.io.*;
import java.util.*;
import java.math.*;

public class Main {

    /**
     * @xqq
     */
    public BigInteger an(int n) {
        BigInteger c;
        BigInteger a = BigInteger.valueOf(1);
        BigInteger b = BigInteger.valueOf(1);
        
        for(int i = 1; i < n; i++) {
            c = a.add(b);
            a = b;
            b = c;
        }
        return a;
    }
    public static void main(String[] args)    throws Exception {
        // 定义并打开输入文件
        Scanner cin = new Scanner(System.in);
        
        Main e = new Main();
        int n;
        
        while(cin.hasNext()) {
            n = cin.nextInt();    
            System.out.println(e.an(n));
        }
        
        cin.close();  //关闭输入文件
    }
}
View Code