Use the factorial formula to calculate the combination:
Programming Ideas:
According to the formula to calculate the size of the combination number, from the keyboard input n,k value, design a calculation of the factorial size, if the input number A is 1 or 0, then directly return 1, otherwise recursion, calculate the factorial of A-1, until a is 1 o'clock, recursion end.
Program Flowchart:
Program Source code:
Public Static void Main (String args[]) { int n, k,c; Scannerin =new Scanner (system.in); System.out.print ("Enter the value of N,k in the number of K selected from N:"); n=in.nextint (); K=in.nextint (); System.out.println ("The result is:"); C=jiecheng (n)/(Jiecheng (k) *jiecheng (nk)); System.out.println (c);}
public static int Jiecheng (int x)
{
int Y=1;
if (x==1| | x==0)
Y=1;
else Y=jiecheng (x-1) *x;
return y;
}
Number of combinations based on Yang Hui triangle recursive computation
Programming Ideas:
According to the law of the Yang Hui Triangle, the value of the nth row of the Yang Hui Triangle is equal to the left and right of the upper row of the element of the position, and then according to the relationship between the Yang Hui Triangle and the combined number, C (n,m) equals the Yang Hui element n+1, according to this, the value of the combined number is written.
Program Flowchart:
Program Source code:
Public Static voidMain (String args[]) {Scanner SC=NewScanner (system.in); System.out.print ("Enter N value:"); intn=Sc.nextint (); System.out.print ("Enter K value:"); intk=Sc.nextint (); int[] A=New int[N+1] [N+1]; for(inti=0;i<n+1;i++) { for(intj=0;j<=i;j++) { if(j==0| | j==i) {a[i][j]=1; System.out.print (A[i][j]+" "); } Else{A[i][j]=a[i-1][j-1]+a[i-1][j]; System.out.print (A[i][j]); System.out.print (" "); }} System.out.println (); } if(n<2| | K==1) System.out.println ("Num=1"); ElseSystem.out.println ("num=" +a[n][k-1]+ "=" +a[n-1][k-2]+ "+" +a[n-1][k-1]); }
Use the formula to calculate the number of combinations:
Programming Ideas:
Input n,m, two numbers (to compose the required number of combinations) (n>m), if m=1, then output n, if m!=1, then go to recursion, use the formula until the N-m=1, the end of recursion, output results.
Program Flowchart
Program Source code:
Public Static voidMain (String args[]) {Scanner SC=NewScanner (system.in); System.out.print ("Enter N value:"); intn=Sc.nextint (); System.out.print ("Enter K value:"); intk=Sc.nextint (); System.out.println ("The result is:" +C (n,k)); } Public Static intCintNintk) {if(n<0| | k<0| | n<k)return0; if(n==k)return1; if(k==1) returnN; returnC (n-1,k) +c (n-1,k-1); }
Implement Hanoi in Java:
Programming Ideas:
1. First enter the number of plates N, if the number of plates is 1, the disc numbered 1 is moved directly from a to C, and the recursion ends.
2. Otherwise:
Recursively, a disc numbered 1 to n-1 on a is moved to B,c as an auxiliary tower;
Direct the disk numbered n from a to C;
Recursively, move the disc numbered 1 to n-1 on B to C,a as an auxiliary tower.
Program Flowchart:
Program Source code:
Public Static voidMain (string[] args) {@SuppressWarnings ("Resource") Scanner SC=NewScanner (system.in); intN; System.out.println ("Please enter the number of your dished (Hanoi Tower):"); N=Sc.nextint (); System.out.println ("The number of the times you need to move the dishes is:" +Newhanoitower (). Hanoitower (n)); Hanoitower Hanoitower=NewHanoitower (); Hanoitower.move (N,' A ', ' B ', ' C '); } Public intHanoitower (intN) {if(n==1)return1; Else returnHanoitower (n-1) *2+1; } Public voidMoveintNCharACharBCharc) {if(n = = 1) System.out.println ("Disk" + N + "moved from + A +" to "+c); Else{Move (n-1, A, C, b); System.out.println ("Disk" + N + "moved from + A +" to "+c); Move (n-1, B, A, c); } }
Randomly enter a string of any size to determine if he is a palindrome string.
Programming Ideas:
From the keyboard arbitrary input a string, and assign it to an array, and then recursive, if i=j, is definitely recursive, otherwise from the array of the first element and the tail element to compare, if equal, then i++ and j--, continue to the middle, until the judgment conditions N>=LEN/2, If there is a non-conformance in the middle of the judgment, directly jump out of recursion, end the process, output is not a palindrome string.
Programming Flowchart:
Program Source code:
Public classhuiwen{Private Static intLen;Private Static Charp[]; Public Static voidMain (String args[]) {Scanner SC=NewScanner (system.in); String str; STR=Sc.nextline (); Len=str.length (); P=Str.tochararray (); if(Huiwen (0)) System.out.println (str+ "is a palindrome!"); ElseSystem.out.println (str+ "is not a palindrome!"); } Public Static BooleanHuiwen (intN) { if(N>=LEN/2) return true; if(p[n]==p[len-1-N])returnHuiwen (n+1); Else return false; } }
Application of "Java" recursive recursion