Definition and use of methods in Java (classroom learning Induction)

Source: Internet
Author: User

Number of combinations, Baidu entry is interpreted as follows: remove the number of all combinations of n (n≤m) elements from m different elements, called the number of combinations of n elements taken from m different elements (combination)

For the calculation of the number of combinations, the need for a certain amount of work, the computer can be very good to help us to select the number of combinations, the following summarizes 3 kinds of JAV calculation combination number method;

One:

The source code is as Follows:

Package Bky_1;import Javax.swing.joptionpane;public class Zuheshu {public static int factorial (int x) {if (x==1| | X==0) {return 1;} return factorial (x-1) *x;} public static void main (string[] Args) {//TODO auto-generated method stubint n,k,w;//the Upper and lower bounds of the combined number, W represents the combined number result String n1,n2; SYSTEM.OUT.PRINTLN ("enter the upper and lower bounds of the number of combinations"); N1=joptionpane.showinputdialog ("enter the upper"); k=integer.parseint (n1); N2=joptionpane.showinputdialog ("enter The lower number"); n=integer.parseint (n2); w=factorial (n)/(factorial (k) * Factorial (n-k)); SYSTEM.OUT.PRINTLN ("combined number result is" +w);}}

Introduced:

Using the method of calculating factorial function, The result of combining number is obtained by Formula C (k/n) =n!/(k!* (n-k)!).

Two:

Source:

Package Bky_2;public class ZUHESHU2 {public static int factorial (int x,int y) {int i,j;int a[][]=new int[20][20];for (i=0;i& Lt;20;i++) {a[i][0]=1;} For (i=1;i<20;i++) {a[i][i]=1;} For (i=2;i<20;i++) for (j=1;j<i;j++) {a[i][j]=a[i-1][j-1]+a[i-1][j];}    Return a[x][y]; }public static void main (string[] Args) {int w;w=factorial (8,2); System.out.println ("number of combinations within 20 lower limit"); System.out.println ("C (2/8) ="); System.out.println (w);}}

Introduction: using the nature of Yang Hui triangle, define two-dimensional array, direct output corresponding combination number Results.

Three:

Source:

Package Bky_3;public class ZUHESHU3 {public static int factorial (int x,int y) {int a[][]=new int [20][20];if (y==0| | X==y) {return 1;} return factorial (x-1,y-1) +factorial (x-1,y);} public static void main (string[] Args) {//TODO auto-generated method     System.out.println ("calculates The number of combinations within 20");    System.out.println ("C (8/2)");    System.out.println (factorial (8,2));}    }

Introduced:

The recursive function is used to get the result of the last combined Number. The recursive function is

public static int factorial (int X,int y)

{int A[][]=new int [20][20];

If (y==0| | X==y) {return 1;}

return factorial (x-1,y-1) +factorial (x-1,y); }

Hanoi: Hanoi (also known as Hanoi) is a puzzle toy from an ancient Indian legend. When big Brahma created the world, he made three diamond pillars, and stacked 64 gold discs on a pillar from bottom to top in order of Size. The great Brahma commanded the Brahman to rearrange the discs from below to the other pillars in order of Size. It is also stipulated that the disc cannot be enlarged on the small disc, and only one disc can be moved between the three pillars at a Time. In java, you can run with a recursive function.

Source:

Towersofhanoi.java//Towers of Hanoi solution with a recursive method.public class towersofhanoi{//recursively move   Disks between towers public static void solvetowers (int disks, int sourcepeg, int destinationpeg, int Temppeg) {//base case--only One disk to move if (disks = = 1) {System.out.printf ("\n%d--and%d",         sourcepeg, destinationpeg);      Return }//end if//recursion step--move (disk-1) disks from sourcepeg//to temppeg using Destinationpeg so      Lvetowers (disks-1, sourcepeg, temppeg, destinationpeg);      Move last disk from Sourcepeg to Destinationpeg System.out.printf ("\n%d-to-%d", sourcepeg, destinationpeg); Move (disks-1) disks from Temppeg to Destinationpeg solvetowers (disks-1, temppeg, destinationpeg, Sour   cepeg); }//end method solvetowers public static void main (string[] Args) {int startpeg = 1;//value 1 used to Indic Ate startpeg in OUTPut int endpeg = 3; Value 3 used to indicate endpeg in output int temppeg = 2; Value 2 used to indicate temppeg in output int totaldisks = 3;      Number of disks//initial nonrecursive call:move all disks.   Solvetowers (totaldisks, startpeg, endpeg, temppeg); }//end Main}//end class Towersofhanoi

"palindrome" is to read the anti-reading can read through the sentence, it is all the time and time there is a rhetorical way and word game, such as "i for everyone, everyone for me" and so On. In mathematics there is such a characteristic of such a number that it becomes a palindrome number (palindrome numbers). In java, a recursive function can be used to determine whether a string is a Palindrome.

Source:

Package Bky_4;import Java.util.scanner;public class Huiwenshu {public static Boolean Huiwenshu (String Text) {int length= Text.length (); for (int i=0;i<length/2;i++) {if (text.charat (i)!=text.charat (length-i-1)) {return false;}} Return true;} public static void main (string[] Args) {//TODO auto-generated method stubSystem.out.println ("please Enter a string:"); Scanner s=new Scanner (system.in); String Str=null;str=s.next ();       If (huiwenshu (str))       {System.out.println ("is a palindrome number");}              Else{system.out.println ("not palindrome number");}}}

Definition and use of methods in Java (classroom learning Induction)

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.