Http://blog.sina.com.cn/s/blog_85790123010155m8.html
Topic Requirements: Write program output Fibonacci number 20 in the console, 5 lines per output
Method One: public class fibonacci1{
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");
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 (); }}}
Method Two: public class fibonacci2{
Defining Array methods
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"); }}}
Method Three: public class Fibonacci3 {
Using recursive methods
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 (); }}
Xiao Kee: beginners java Edge began to contact the Fibonacci sequence problem (1,1,2,3,5,8,13,21 such a series, starting from the third number of each number equal to its previous two numbers added), began to understand, Later, when doing a rabbit-related exercise, I got some ideas (the essence of this rabbit problem is the Fibonacci sequence: There are a pair of rabbits, from the 3rd month after the birth of a pair of rabbits each month, the rabbit long to 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 much?), Now from the variable, array, recursive three point of view to solve this puzzle, of course, there are other methods, the same problem with a variety of ideas to think about solving, but also to the comprehensive use of knowledge exercise bar.