Topic Requirements: Write program output Fibonacci number 20 in the console, 5 lines per output
Java programming: Three ways to implement the Fibonacci sequence
One way:
public class Demo2 {
//define three variable methods public
static void Main (string[] args) {
int a = 1, b = 1, c = 0;
System.out.println ("The first 20 items of the Fibonacci series are:");
System.out.print (A + "T" + B + "T");
Because there are two 1, 1 in front, so i<=18 for
(int i = 1; I <= i++) {
c = a + B;
A = b;
b = C;
System.out.print (c + "T");
if ((i + 2)% 5 = 0)
System.out.println ();
}
}
Java programming: Three ways to implement the Fibonacci sequence
The second method:
public class Demo3 {
//definition array method public
static void Main (string[] args) {
int arr[] = new INT[20];
Arr[0] = arr[1] = 1;
for (int i = 2; i < arr.length i++) {
arr[i] = arr[i-1] + arr[i-2];
}
System.out.println ("The first 20 items of the Fibonacci series are as follows:");
for (int i = 0; i < arr.length i++) {
if (i% 5 = 0)
System.out.println ();
System.out.print (Arr[i] + "\ t");}}
Java programming: Three ways to implement the Fibonacci sequence
Its three methods:
public class Demo4 {
//Use recursive method
private static int Getfibo (int i) {
if (i = = 1 | | | i = = 2) return
1;
else return
Getfibo (i-1) + Getfibo (i-2);
}
public static void Main (string[] args) {
System.out.println (the first 20 items of the Fibonacci series are:);
for (int j = 1; J <= J + +) {
System.out.print (Getfibo (j) + "T");
if (j% 5 = 0)
System.out.println ();
}
}
The essence of this rabbit problem is the Fibonacci sequence: has a pair of rabbits, from the 3rd month after the birth of a pair of rabbits each month, small rabbit after the third month after the birth of a pair of rabbits each month, if the rabbit is not dead, ask the number of rabbits each month for how many?, now from variables, arrays, Recursive three angles to solve this puzzle, of course, there are other methods, the same problem with a variety of different ideas to think about solving, but also to the comprehensive use of knowledge exercise bar.