Java Fibonacci numbers

Source: Internet
Author: User

1599: Fibonacci numbers

Time Limit: 1 sec memory limit: 128 MB
Submit: 53 solved: 23
[Submit] [Status] [web board]

Description

A Fibonacci sequence is calculated by adding the previous two members of the sequence,
The RST two members being both 1.
F (1) = 1; F (2) = 1; F (n> 2) = f (N 1) + f (N 2)

Input

Your task is to take a number as input, and print that bonacci number.

Output

Print that bonacci number.

Sample input100
Sample output354224848179261915075
1 import java. Math. bigdecimal;
2 Import java. util. collections;
3
4 public class Fab {
5 /*
6 * First, use the reserved word class to declare a new class, and it is a public class.
7 */
8
9 public static void main (string ARGs []) {
10 /*
11 * The main method is defined in this class. Public indicates the permission, public, and static.
12 * If a member is declared as static, it can be accessed before any object of its class is created without referencing any object. You can declare both methods and variables as static.
13 * The most common example of static members is main (). Because the main () must be called when the program starts execution, it is declared as static. Void
14 * indicates that the return value of the main function is null.
15 */
16 CIN = new partition (system. In );
17 /*
18 * inputs-the input handler class in the console is a new class added by jdk5. it mainly serves to process input streams, files, and text content.
19 */
20 int N, I;
21 bigdecimal f [] = new bigdecimal [6000];
22 /*
23 * dynamic initialization *
24 * dynamic initialization, that is, only specifying the length of the array and applying for space in the memory. Dynamic initialization does not have to be put together with the array declaration, or you can reinitialize an initialized array.
25 * syntax format for dynamic initialization: data type array name [] = new data type [length]
26 */
27 F [1] = bigdecimal. valueof (1 );
28 F [2] = bigdecimal. valueof (1 );
29 /*
30 * assign a value to a large number
31 */
32 For (I = 3; I <= 5000; I ++)
33 F [I] = f [I-1]. Add (F [I-2]);
34 while (CIN. hasnext ()){
35 /*
36 * is equivalent! = EOF
37 */
38 N = cin. nextint ();
39 system. Out. println (F [N]);
40
41}
42}
43
44}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.