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}