首页 > 代码库 > UVA10334 - Ray Through Glasses(Fibonacci数列)

UVA10334 - Ray Through Glasses(Fibonacci数列)

UVA10334 - Ray Through Glasses(Fibonacci数列)

题目链接

题目大意:求光穿过两块玻璃的折射次数的方式数目,我也有点不懂它说什么,但是看图看明白了。

解题思路:画画折射3,4,5,发现话的规律,然后就会发现符合Fibonacci数列,f[n] = f[n - 1] + f[n - 2];但是n很大到1000,需要用大数。

代码:

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

public class Main {

    static BigInteger f[] = new BigInteger[1005];

    public static void init() {

        f[0] = BigInteger.valueOf(1); 
        f[1] = BigInteger.valueOf(2);
        for (int i = 2; i <= 1000; i++)
            f[i] = f[i - 1].add(f[i - 2]);
    }

    public static void main(String args[]) {

        Scanner cin = new Scanner(System.in);
        int n;
        init();

        while (cin.hasNext()) {

            n = cin.nextInt();
            System.out.println(f[n]);
        }
    }
}

UVA10334 - Ray Through Glasses(Fibonacci数列)