hdu1250(Java)大数相加的问题
2016-07-17 22:36
399 查看
Hat's Fibonacci
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10525 Accepted Submission(s):
3483
[align=left]Problem Description[/align]
A Fibonacci sequence is calculated by adding the
previous two members the sequence, with the first two members being both
1.
F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) +
F(n-3) + F(n-4)
Your task is to take a number as input, and print that
Fibonacci number.
[align=left]Input[/align]
Each line will contain an integers. Process to end of
file.
[align=left]Output[/align]
For each case, output the result in a line.
[align=left]Sample Input[/align]
100
[align=left]Sample Output[/align]
4203968145672990846840663646
-------------------------------------------------
import java.math.BigDecimal;
import java.util.Scanner;
public class Main1250{
public static void main(String args[]){
Scanner cin=new Scanner(System.in);
BigDecimal []a=new BigDecimal[10007];
a[0]=new BigDecimal(1);
a[1]=new BigDecimal(1);
a[2]=new BigDecimal(1);
a[3]=new BigDecimal(1);
for(int i=4;i<10007;i++)
a[i]=a[i-1].add(a[i-2]).add(a[i-3]).add(a[i-4]);
while(cin.hasNext()){
int n=cin.nextInt();
System.out.println(a[n-1]);
}
}
}
-----------------
Note: No generated Fibonacci number in excess of 2005 digits will be in the test data, ie. F(20) = 66526 has 5 digits.
这个地方要注意数组要开大点,题目不是说只有2005个数
相关文章推荐
- java基础知识
- 遍历map的方法
- Java类库及其组织结构
- 数组中重复的数字 java
- RxJava 与 Retrofit 结合的最佳实践
- Java IO操作——字节流(OutputStream、InputStream)和字符流(Writer、Reader)
- JavaWeb(四)
- JavaWeb(三)
- Java线程详解
- javaee之Servlet的理解
- javaweb之请求转发和请求包含
- listview 加载的时候报java.lang.IllegalArgumentException: column ‘_id’ does not exist 错误
- JAVA学习总结二十六
- Java-算法之选择排序
- JAVA学习总结二十五
- 建立java环境
- 字符流中第一个不重复的字符 java
- java积累——load-on-startup在web.xml中的作用
- JDK的命令行工具
- java基础--07成员变量和局部变量