Java applet (ii)

Source: Internet
Author: User

4. Output the first 20 elements of the Fibonacci sequence in the Java language:

(1). Using the array method to achieve the output of the Fibonacci sequence, the code is as follows:

1  Public classDemo {2      Public Static voidMain (String args[]) {3         4         intarr[]=New int[20];5Arr[0]=1;6Arr[1]=1;7System.out.print (arr[0]+ "");8System.out.print (arr[1]+ "");9          for(inti=2;i<20;i++){TenArr[i]=arr[i-1]+arr[i-2]; OneSystem.out.print (arr[i]+ ""); A         } -     } -  the}

(2). Use the variable method to achieve the output of the Fibonacci sequence, the code is as follows:

1  Public classDemo {2      Public Static voidMain (String args[]) {3 4         intNum1=1;5         intNum2=1;6System.out.print ("" +num1);7System.out.print ("" +num2);8          for(inti=3;i<=20;i++){9             intnum3=num1+num2;Tennum1=num2; OneNum2=num3; ASystem.out.print ("" +num3); -         } -     } the  -}

(3). Use the recursive function method to achieve the output of the Fibonacci sequence, the code is as follows:

1  Public classDemo {2      Public Static voidMain (string[] args) {3         4          for(inti=1;i<=20;i++){5System.out.print ("" +f (i));6         }7     }8      Public Static intFintN) {9         if(n==1| | n==2) Ten             return1; One         Else A             returnF (n-1) +f (n-2); -     } -  the}

5. For a 4*3-order matrix transpose, the matrix transpose is the matrix row, column interchange, the code is as follows:

1  Public classMatrixdemo {2      Public Static voidMain (String args[]) {3         4         inti,j;5         intarr1[][]={{1,2,3},{4,5,6},{7,8,9},{10,11,12}};6         intarr2[][]=New int[3] [4];7         8System.out.println ("Before Matrix Transpose:");9          for(i=0;i<4;i++){Ten              for(j=0;j<3;j++){ OneSystem.out.print (arr1[i][j]+ ""); A             } - System.out.println (); -         } the          -System.out.println ("After matrix transpose:"); -          for(i=0;i<4;i++){ -              for(j=0;j<3;j++){ +arr2[j][i]=Arr1[i][j]; -             } +         } A          for(i=0;i<3;i++){ at              for(j=0;j<4;j++){ -System.out.print (arr2[i][j]+ ""); -             } - System.out.println (); -         } -     } in      -}

6. Find the largest and smallest element in the 4-order matrix and the column number of the row number, the code is as follows:

1  Public classMaxmindemo {2      Public Static voidMain (String args[]) {3         4         intarr[][]={{36,45,90,3},{24,39,10,4},{36,72,66,5},{64,88,120,6}};5         intI,j,m=0,n=0,a=0,b=0;6         intMax=arr[0][0],min=arr[3][3];7         8System.out.println ("4-Order matrix:");9          for(i=0;i<4;i++){Ten              for(j=0;j<4;j++){ OneSystem.out.print (arr[i][j]+ ""); A                  -                 if(max<Arr[i][j]) { -max=Arr[i][j]; theA=i; -b=J; -                 } -                 if(min>Arr[i][j]) { +min=Arr[i][j]; -m=i; +n=J; A                 } at             } - System.out.println (); -         } -SYSTEM.OUT.PRINTLN ("Maximum number of max= in matrix" +max); -System.out.println ("The number is in the matrix" +a+ "line" + "+b+" column "); -SYSTEM.OUT.PRINTLN ("Minimum number of min= in matrix" +min); inSystem.out.println ("The number is in the matrix" +m+ "line" + "+n+" column "); -     } to  +}

Java applet (ii)

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.