hdu1250(Java)大数相加的问题

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

Problem Description

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.

Input

Each line will contain an integers. Process to end of
file.

Output

For each case, output the result in a line.

Sample Input

100

Sample Output

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个数

时间: 2024-11-02 13:13:51

hdu1250(Java)大数相加的问题的相关文章

HDU 1250 Hat&#39;s Fibonacci(Java大数相加)+讲解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1250 Problem Description 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 -

hdu1715(Java)大数相加

大菲波数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17352 Accepted Submission(s): 5778 Problem Description Fibonacci数列,定义如下:f(1)=f(2)=1f(n)=f(n-1)+f(n-2) n>=3.计算第n项Fibonacci数值. Input 输入第一行为一个整数N,接

POJ 1503 Integer Inquiry(大数相加,java)

题目 我要开始练习一些java的简单编程了^v^ import java.io.*; import java.util.*; import java.math.*; public class Main { /** * @param args */ public static void main(String[] args) throws Exception { // 定义并打开输入文件 Scanner cin = new Scanner(System.in); BigInteger a, sum

Java之大数相加

之前参加某公司笔试,机试题目是大数相加,两大数是字符串形式,求和. 当时讨巧用的是BigDecimal类,但是发迷糊了,以为b1.add(b2)后,和就加到b1上了,结果一直输出不对. 其实应该是这样: private static void add2Sum(String s1, String s2) { System.out.println("-----------"); BigDecimal b1 = new BigDecimal(s1); BigDecimal b2 = new

大菲波数(Fibonacci)java大数(hdu1715)

大菲波数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11520 Accepted Submission(s): 3911 Problem Description Fibonacci数列,定义如下:f(1)=f(2)=1f(n)=f(n-1)+f(n-2) n>=3.计算第n项Fibonacci数值. Input 输入第一行为一个整数N,接

java大数--总结

BigInteger(高精度整数) 1.所在包: java.math.BigInteger 2.大数运算,以下返回类型均为BigInteger BigInteger a; BigInteger b; a.add(b);//大数相加 a.divide(b);//a整除以b a.mod(b);//a对b取余 3.基本类型转化成大数 BigInteger x = BigInteger.valueOf(n); 4.大数求最大公约数 BigInteger a; BigInteger b; a.gcd(b)

java大数的基本函数

1.读入 Scanner cin=new Scanner(System.in);// 读入 while(cin.hasNextInt()) //等同于!=EOF,第一数一定要输入整形的 { } 大数的一般是: while(cin.hasNextBigInteger())  //第一个数一定要输入大数的 { } while(t-->0)   //等同于while(t--) { } 2.赋值 BigInteger b=BigInteger.valueOf(a); //a可为int,long,stri

hdu acm-1047 Integer Inquiry(大数相加)

Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11678    Accepted Submission(s): 2936 Problem Description One of the first users of BIT's new supercomputer was Chip Diller. He ex

HDU 1047。多个大数相加

Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13496    Accepted Submission(s): 3390 Problem Description One of the first users of BIT's new supercomputer was Chip Diller. He e

栈的应用—大数相加

package org.Stone6762.MStack.adopt;    import java.util.Scanner;    import org.Stone6762.MStack.imple.LinkStack;    /**  * @author_Stone6762  * @Description_大数相加  * */  public class BigAdd {        /**      * @Describe_将表示数字的字符串从高位到低位的形式压入栈_并去除其中的空格