Java jobs use recursion to solve problems

Source: Internet
Author: User

First question

Using recursion to find the number of combinations

Design ideas

(1) First, according to the formula, using recursion to complete the initialization of the factorial function, and by calling factorial, to achieve the formula calculation

(2) Recursive method, according to the characteristics of the Yang Hui Triangle, set up a two-dimensional array, from top to bottom to save the Yang Hui Triangle, and each judgment, the row and the user want to get the number of the row is the same

(3) Recursive method, recursive call function, through the recursive formula from the forward derivation of c[n,k]=c[n-1,k-1]+c[n-1,k], according to the formula until N=1 or k=0; or n=k equal to the end

Program Flowchart

Program source Code


Import Java.util.Scanner;

Public class Yanghui {
public int jiecheng (int i) {//recursive factorial
if (i==1| | i==0)
{
return 1;
}
return I*jiecheng (i-1);

}
public int ditui (int i,int j)
{
int a[][]=new int[100][100];//Declare a two-dimensional array to store the Yang Hui triangle
a[0][0]=1;
I NT Result=1; Used to save the result
for (int. p=1;p<100;p++)
for (int q=0;q<=p;q++)
{
if (q==0| | P==Q)///Yang Hui the number on the two-waist of the triangle is 1
{
a[p][q]=1;
}
Else
A[p][q]=a[p-1][q-1]+a[p-1][q];//Recursive Results
if (P==I&&Q==J)//Determine if the result is to be output
{
Result=a[p][q ];
break;
}

}

return result;
}
public int Zuhe (int i,int j)//Subscript for combinatorial number formula
{
if (i==0| | j<=1| | I==J)
{
return 1;
}
Return Zuhe (i-1,j-1) +zuhe (I-1,J);
}
public static void Main (string[] args) {
TODO auto-generated Method Stub
Yanghui yh=new Yanghui ();
System.out.println ("Please enter the number of combinations subscript and row label (separated by a space or carriage return)");
Scanner input=new Scanner (system.in);
int Hang=input.nextint (); The subscript of the combined number
int Lie=input.nextint (); Combination number Superscript
int Fenzi,fenmu;
Fenzi=yh. Jiecheng (hang); Molecules that use factorial methods to calculate results
Fenmu=yh. Jiecheng (Lie) *yh. Jiecheng (Hang-lie); Calculate denominator
System.out.println ("Using factorial calculation result:" +fenzi/fenmu+ ""); Output results
System.out.println ("Using recursion to calculate results:" +yh. Ditui (hang, lie) + "");
System.out.println ("Computes the result by recursion:" +yh. Zuhe (hang+1, lie+1) + "");

}

}

Test results

Second question

Hanoi Tower Problem

Design ideas

The title asks for n plates to use the number 2nd pole from 1th to 3rd, first of all, if it is two plates, is to put the first one to 2. The second one put to three, and then the second number to third. If it is three will be the top two with the help of number third to second, and then put the last one to third, in the use of the number second number third. In turn. Use recursive implementations. is to start with n plates, move the n-1 to number second, move the last one to three, and move the number second to three. So N-1 moved to number second, knowing that there is only one plate left, and the algorithm is over.

Program Flowchart

Program source Code


Import Java.util.Scanner;

public class Hannt {
public void Hnt (int a,char One,char Two,char three) {//move the one on one to three
if (a==1)
Move (One,three);
Else
{
Hnt (A-1,one,three,two); Move from one to the other with three
Move (One,three);
Hnt (A-1,two,one,three);
}
}
public void Move (char X,char y) {
System.out.println (x+ "-" +y); Move from X to Y
}

public static void Main (string[] args) {
TODO auto-generated Method Stub
Hannt h=new hannt ();
Scanner input=new Scanner (system.in);
System.out.print ("Please enter the initial plate quantity:");
int Num=input.nextint ();
H.hnt (num, ' 1 ', ' 2 ', ' 3 ');

}

}

Test results

Third question

Check whether the string is a palindrome number

Design ideas

Count the string length, then define two numbers, one to save the initial position, the second to save the end position, to extract the relative position characters from the string, and to match the same, minus two for each execution, until the length is zero or one, and the result is output.

Program Flowchart

Program source Code

Import Java.util.Scanner;

public class Huiwen {
public int Hw (String str,int length,int star,int stop) {
if (length==0| | Length==1)
{
return 1;
}
if (Str.charat (Star)!=str.charat (stop))
{
return 0;
}
Else
{
Return Hw (str,length-2,star+1,stop-1);
}

}

public static void Main (string[] args) {
TODO auto-generated Method Stub
Scanner input=new Scanner (system.in);
String S=input.nextline ();
Huiwen hw=new Huiwen ();
int L=s.length ();
if (HW. Hw (S, l, 0, l-1) ==1)
{
System.out.println ("is a palindrome string");
}
Else
System.out.println ("Not a palindrome string");

}

}

Test results

Summarize

These three questions are based on the recursive completion, in the process of errors encountered in most of the recursion can not jump, the end of the condition is incomplete resulting in direct error. So the next time you should examine the end condition carefully, this is the most important.

Java jobs use recursion to solve problems

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.