How to implement multiplication principle using Java language

Source: Internet
Author: User

Let's start by explaining several concepts:
Multiplication principle: Do one thing, complete it need to divide into n steps, do first step have M1 different method, do second step have m2 different method, ..., do nth step have mn different
method, there are n=m1m2m3...mn different ways to complete the event. This is the understanding in the probability study
Permutation: Generally, the M (m≤n) elements are taken out of n different elements, arranged in a certain order, called an array of m elements from n elements.
Full arrangement: N A permutation of all the different elements taken out, called a full permutation of n different elements. This is the understanding in combinatorial mathematics.
No matter what the understanding: algebra can be expressed as a number of multiplication, for the convenience of research, we assume that we have to use a program to solve the factorial within 5 (five elements within the full array). It's easy to find factorial, but what if we ask to show each of these different permutations?

For example, "ABCD", the program perfection the number of permutations, and display each element.
For this question: Let's start with a simple analysis:
If the perfection row number, we can use the method of the previous log to solve:
sum (int n) {
if (n==0)//math has a 0 factorial of 1
return 1;
Else
Return N*sum (n-1);
}
Analysis of the second question, since it is all arranged, for the ABCD of the four letters, we just let each letter when the first line, that is, 4, plus three letters in the full arrangement, and so on, to the left of a letter, then it is a sort of, that is, its own
Similarly, we use recursive thinking to complete the programming of this program, because to the left of a letter, that is what we call recursion in the termination condition.
First write a Method GetString () to get the string we want to arrange
public static String getString () throws ioexception{
InputStreamReader ISR = new InputStreamReader (system.in);
BufferedReader br = new BufferedReader (ISR);
String str = br.readline ();
return str;
}
Or
public static String getString () {
Scanner scan = new Scanner (system.in);
String str = scan.nextline ();
return str;
}
As for the difference between the two methods, the latter article will explain.
We have a doanagram () to complete our recursion
public static void Doanagram (int newSize) {//parameter is the length of the string
if (newSize = = 1)
Return If it is a letter, the method returns
for (int j=0; j<newsize; j + +) {
Doanagram (newSize-1);//recursive, calling its own method
if (newsize==2)//The condition used to remove duplicate elements
Displayword ();//method of displaying elements
Rotate (newSize);//specific method of element arrangement
}
}
The following is the Rotate method
public static void rotate (int newSize) {
Int J;
int position = Size-newsize;
Char temp = arrchar[position];
for (j=position+1; j<size; j + +)
ARRCHAR[J-1] = Arrchar[j];
ARRCHAR[J-1] = temp;
}

The method is understood as follows:
There are four positions 0 1 2 3 and one temp location
Their initial order was temp 0 1 2 3
A B c D
First time: Temp 0 1 2 3
A B c D
Second time: Temp 0 1 2 3
A B c D
Last: Temp 0 1 2 3
b c D A
System.out.print (++count+ "");
for (int j=0; j<size; j + +)
System.out.print (Arrchar[j]);
System.out.print ("");
System.out.flush ();
if (count%6 = = 0)
System.out.println ("");
Show Displayword methods as follows
public static void Displayword () {
}
With all of the above methods, we can get all the permutations of the strings we have entered.





Here's a way to share a netizen's writing
/** * Recursive Call method
* @param parameter need to arrange the parameters
* @return This parameter to arrange all possible arrays
* Because the result may have a number of repetitions, so the use of set to weight
*/
private static set<string> action (String parameter) {
If the argument is 1, it is returned,
The length of each recursive parameter is reduced by 1, which is the last node of the recursion
if (parameter.length () = = 1) {
set<string> set = new hashset<string> ();
Set.add (parameter);
return set;
}
method returns an array
set<string> resultlist = new hashset<string> ();
A loop parameter that divides it into two segments, one for a single character, and one for the rest of the string
for (int i=0;i<parameter.length (); i++) {
Single character, if 12345 needs to be arranged, first 1 motionless, 2345 arranged
String s = parameter.substring (i,i+1);
The rest of the string
String rest = parameter.substring (0,i) +parameter.substring (i+1);
Recursion the rest of the string to the resulting array
set<string> list = action (rest);
Combine this single character with the rest of the string, completing both the first bit of the character and the arrangement of the remaining strings
for (String str:list) {
StringBuilder sb = new StringBuilder (S.length () +str.length ());
Sb.append (s);
Sb.append (str);
Resultlist.add (Sb.tostring ());

}
}
return resultlist;
}


This article is from the "Steel Coins Son" blog, please be sure to keep this source http://gangbenger.blog.51cto.com/9581348/1638417

How to implement multiplication principle using Java language

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.