题目大意:输入一个整数t表示测试用例数。接着输入n,输出对应的斐波那契数fib[n]。(一开始,我看成了输入n,然后输出前n个斐波那契数。。。蛋碎了一地)
解题思路:对于用大数来解决的斐波那契数列的相关题目。我们都可以先根据范围创建好数组。然后在需要用的时候,直接在
数组里面取需要用的数即可
代码如下:
package com.njupt.bigInteger; import java.math.BigInteger; import java.util.Scanner; public class HDU_1715_2 { public static void main(String[] args) { Scanner scanner = new Scanner(system.in); BigInteger fib[] = new BigInteger[1001]; fib[1] = new BigInteger("1"); fib[2] = new BigInteger("1"); /** * 现根据范围创建好斐波那契数列 */ for (int i = 3; i <= 1000; ++i) { fib[i] = fib[i - 1].add(fib[i - 2]); } while (scanner.hasNextInt()) { int t = scanner.nextInt(); while (t > 0) { int i = scanner.nextInt(); System.out.println(fib[i]); t--; } } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。