Java implementation of M-number of all permutations and the number of N (ordered) from M

Source: Internet
Author: User

(1) The recursive law of the whole permutation combination:

Set S full permutation combination all (s) =n+all (n-N);

With the set s={1,2,3} as an example, the full permutation of S is all (s) ={1}+all ({2,3}), where n={1},s-n={2,3}

From the above example, we can know that the above algorithm can be solved by recursion.

We take the extreme case, if the set S is empty, then there is no need for recursion.

All permutations, if the set has 4 elements, the number of total permutations is a (=4*3*2*1=24), and the code is as follows:

Package Datastructer;import Java.util.arrays;import Java.util.linkedlist;import Java.util.list;public class fullpermutation {//full permutation combination private int n;public fullpermutation () {this.n=0;} Public  void Listall (List candidate,string prefix) {if (Candidate.isempty ()) {System.out.println (prefix); this.n++ ;} for (int i=0;i<candidate.size (); i++) {List temp=new LinkedList (candidate);//Convert to Linklist, removing an object without affecting the original queue    string S1=prefix+temp.remove (i);//used to save the result of the permutation combination Listall (TEMP,S1);//Note that both temp and S1 are completely new collections and strings, not always working on a collection}} public int Getn () {return n;} public void Setn (int n) {THIS.N = n;} public static void Main (string[] args) {String []arr={"1", "2", "3", "4"}; Fullpermutation f=new fullpermutation (); F.listall (Arrays.aslist (arr), ""); System.out.println ("All permutations:" +F.GETN ());}}

(2) Select n data in M data set (ordered)

The law of N data in M data sets is: Get ({m},n) =t+get (Size of collection {m-t},m-t)

Consider extreme situations, if only one element is taken in the set m, then the element can be removed directly.

How to assemble 4 elements, and take out two ordered elements of a (4,2) =4*3=12

Package Datastructer;import Java.util.arraylist;import Java.util.arrays;import java.util.linkedlist;import Java.util.list;public class Man {private int all;public man () {this.all=0;} public int GetAll () {return all;} public void SetAll (int. all) {This.all = all;} public static void Main (string[] args) {string[] n ={"1", "2", "3", "4"};man m=new Man (); List lst = arrays.aslist (n);        M.take ("", 2,lst);        System.out.println (M.getall ());} Public  Void Take (String s, int total, List lst) {for (int i = 0; i < lst.size (); i++) {//system.out.println ("i=" +i) ; List templst=new LinkedList (LST); string n =  (string) templst.remove (i);//The number taken out of string str = s + n;if (total = = 1) {System.out.println (str);//with the most extreme n of Take only one, directly to the output of the results can be//total=all;all++;} else {int temp=total-1;//cannot be reduced on the same level as total totals, and cannot be based on the original variable (str, temp, templst);//Temp and templst Here are all new variables and sets}}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java implementation of M-number of all permutations and the number of N (ordered) from M

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.