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

时间:2014-05-01 18:55:52   收藏:0   阅读:450

题目

 

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

 

mamicode.com,码迷
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

 

zoj Fibonacci Numbers ( java , 简单 ,大数),码迷,mamicode.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!