Recursive recursive combination number, Hanoi, palindrome number problem (Java)

Source: Internet
Author: User

Recursive recursive combination number:

1 Ideas : using functions to obtain n! , call the function calculation result flowchart

2.1 flowchart

3 . 1 Source code:

import Java.util.Scanner;

Public class N

Public Static void Main (String [] args) {

int n,k;

int S

System. out. println ("Please enter the values of N and K in C (n,k):");

Scanner num = new Scanner (System. in);

N=num.nextint ();

K=num.nextint ();

s= Fun (n)/(Fun (k) *Fun (n-k));

System. The result of out. println ("C (" +n+k+ ") is:" +s);

}

Static int Fun (int a)

{

int m=1;

for (int b=1;b<a;b++) {

m=m* (b+1);

}

return m;

}

}

4.1 Results

2.2 Flowchart

3.2 Source code:

import Java.util.Scanner;

Public class Fact {

Static int Fun (int a) {

if (a==0| | A==1) {

return A;

}

Else {

return a* Fun (A-1);

}

}

Public Static void Main (String[]args) {

int n,k;

System. out. println ("Please enter N and K");

Scanner num = new Scanner (System. in);

N=num.nextint ();

K=num.nextint ();

int C

C= Fun (n)/(Fun (k) *Fun (n-k));

System. out. println ("Result:" +c);

}

}

4.2 Results:

2.3 Flowchart:

3.3 Source code:

import Java.util.Scanner;

Public class Yanghui {

Public Static void Main (String []args) {

int n = 0,k;

int b[][];

System. out. println ("Please enter the value of N:");

Scanner num = new Scanner (System. in);

N=num.nextint ();

K=num.nextint ();

b = new int[n][];

for (int i=1;i<=n;i++) {

B[I-1] = new int[i];

}

for (int j=0;j<n;j++) {

for (int z=0;z<=j;z++) {

if (j==0| | z==0| | Z==J) {

B[j][z]=1;

continue;

}

Else {

B[J][Z] = b[j-1][z-1]+b[j-1][z];

}

}

}

for (int x=0;x<n;x++) {

for (int y=0;y<=x;y++) {

System. out. Print (b[x][y]+ "");

}

System. out. println ("");

}

int S

S=B[N-1][K-2]+B[N-1][K-1];

System. out. println ("And yes:" +s);

}

}

4.3 Results:

Hanoi: (Baidu's)

Flow chart:

Source:

import Java.util.Scanner;

Public class Hannuota {

Public Static void Main (string[] args)

{

Hannuota h=New Hannuota ();

H.go ();

}

Public void Go ()

{

System. out. println ("Please enter the number of plates N:");

@SuppressWarnings ("resource")

Scanner scanner=New Scanner (System. in);

int n=scanner.nextint ();

if (n>=0)//Determine whether n meets the requirements

{

System. out. println (the steps for "moving" +n+ "plates are:");

Hanoi (N, ' A ', ' B ', ' C ');

}

Else System. out. println ("The number of inputs does not meet the requirements");

}

Public void Hanoi (int m,Char one,char one, char three)

{

if (m==1)

Move (One,three);

Else

{

Hanoi (M-1,one,three,two);//The first step is to move a n-1 plate on a C to B

Move (One,three);//move a plate to C on the second step

Hanoi (M-1,two,one,three);//The third step is to move a n-1 plate on B to C

}

}

Public void Move (char x,char y)

{

System. out. println (x+ "-" +y);

}

}

3 results:

Palindrome:

1 design ideas:

Defines the string, gets the length, and uses the recursive function to determine whether the two ends are equal.

2 Flowchart:

3 Source code:

Import Java.io.BufferedReader;

Import java.io.IOException;

Import Java.io.InputStreamReader;

public class Huiwen {

public static void Main (string[] args)

{

Huiwen h=new Huiwen ();

try {

H.go ();

} catch (IOException e) {

E.printstacktrace ();

}

}

public void Go () throws IOException

{

String Input=null;

System.out.println ("Input string:");

BufferedReader is=new BufferedReader (New InputStreamReader (system.in));

Input=is.readline ();

if (!input.isempty ())

{

int [] array=new int[input.length ()];

for (int i=0;i<input.length (); i++)

{

Array[i]=input.charat (i);

}

System.out.println ("is a palindrome number?") "+ifhuiwen (array,0));

}

Else System.out.println ("The number of inputs does not meet the requirements");

}

public boolean Ifhuiwen (int[] a,int L)

{

if (L==A.LENGTH/2)

return true;

if (A[l]==a[a.length-l-1])

Return Ifhuiwen (a,l+1);

else return false;

}

}

4 results:

Recursive recursive combination number, Hanoi, palindrome number problem (Java)

Related Article

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.