JavaBean that implements arrangements and combinations

zhaozj2021-02-12  143

JavaBean that implements arrangements and combinations

August 07, 2002 20:28:38

LinuxAid

When we program, it is often involved in the arrangement and combination. So how should I achieve in Java? In fact, this problem is the first is the problem of algorithms, which is clearly understood. What programming languages ​​is not so important. First, the so-called all-alone, which is the n object, list all of its possible arrangements. This problem is relatively simple. Let us start from the easiest situation, if we only have an object, how do you implement its full arrangement? The answer is simple, now we have only one arrangement. OK, now we tried to introduce the second object, first we need to know, now add a few full-rated possibilities, we can see, the introduction of the n-1 object, the full arrangement of N-1 object The impact is to insert the nth object in any of the previously arranged positions, that is, ListCount (n) = listcount (n-1) * n; and according to this idea, for any N object Arrange, we can start from the simplest number of objects until all N objects are generated. Second, the combination of so-called combinations is all possible to remove all possible M objects from n objects. This problem may be complicated. For a combination, the order of the object is meaningless, 1, 2, 3, and 3, 2, 1 are the same possibilities. Then we need to develop a rule for people, we can envisure the continuous serial number to each object, and the object in our combination must be arranged in the order of its serial number, so we can effectively avoid the order of order. Impact. Assume that we have six objects now, the serial number is 1, 2, 3, 4, 5, 6. Then our first combination is 1, 2, 3, and the last combination is 4, 5, 6. In this case, we have experienced other possible 18 cases that we have in this algorithm, we naturally think of using recursive functions. First we first define the first one in our results, first may be 1, 2, 3, and then set the current location as the last bit, as long as it is possible, for the last one, its maximum value can only be 6 Before that, before the 6th, a new combination is added. If the last bit is already 6, we turn the current position to the previous one, the maximum value of the former one is 5, if the previous one Not 5, then add one, then the current position is again transferred to the last bit, and now the last bit is 4, starting from 4 until 6, and has formed our new combination, so until When we eventually appear 4, 5, 6, the function exits.

Here is our Java source program: mytest.java: package customer, {string [] mychar = new string [50]; int carcount; int carlist; public void setMytest () // initialization {charcount = 0; charlist = 1; public void insertchar (String thischar) // Add a new string {Charcount ; mychar [charcount] = thischar; charlist * = charcount;} public string listallchar () // list full ranked {string [] [ ] allchar = new string [charList 1] [CharCount 1]; INT i; int J; int z = 1; for (i = 1; i <= charcount; i ) {MyCopy (AddCharlist (i, allchar, z ), allchar, charlist, charcount; z * = i;} string listallchar = new string (""); for (i = 1; i <= charlist; i ) {for (j = 1; j <= charcount; J ) listallchar = allchar [i] [j] ""; listallcha = listallchar "" "" "";} return listallchar;} public string [] [] addcharlist (int i, string [] [] allchar, int z) // I-1 Objects Introduced INT {INT J; INT H = 1; INT K; String [] [] Tempallchar = New String [Charlist 1] [Charcount 1]; for (k = 1; K <= z; k ) {for (j = 1; j <= i; j ) {mycopy (Tempchar (j, allchar [k], mychar [i]), Tempallchar [h], charcount); h }} return tempallcha;} public String [] tempchar (int i, string [] beginchar, string thischar) // Plug new object into the specified position {Int J; string [] tempbeginchar = new string [charcount 1]; mycopy; beginchar, tempbeginchar, charcount; For (j = charcount; j> i; j -) TempbeginChar [J]; TempbeginChar [J-1]; TempBeginChar [I] = thischar; return tempbeginchar;} public string selectsomechar (int search) // list it All combinations of SELECT object {intate = 1; int =; for (i = select 1; i <= charcount; i ) selectcount = selectcount * I / (i-select);

String [] selectchar = new string [selectcount 1] [select 1]; int [] [] selectint = new int [selectcount 1] [SELECT 1]; for (i = 1; i <= SELECT ; i ) {selectchar [1] [i] = mychar [i]; selectint [1] [i] = i;} int z = 1; addselect (selectchar, selectint, 1, select, select); int J; string SelectSomeChar = new string (""); for (i = 1; i <= selectcount; i ) {for (j = 1; j <= select; j ) selectsomechar = selectchar [i] [j] "" "" = SelectSomeChar ""} return selectsomechar;} public void addselect (String [] SelectChar, int [] SelectInt, int z, int position, int search // Add new combination {INT i; if (Position == SELECT) {if (selectint [z] [position] {z ; mycopy (selectint [z-1], selectint [z], select); selectint [z] [select] ; for (i = 1; I <= SELECT; i ) SelectChar [z] [i] = mychar [selectint [z] [i]]; addselect (selectchar, selectint, z, position, select);} else {position -; addselect (selectchar, SelectInt, Z, Position, SELECT);}} else {}} else {}} else {}} else {} (selectint [z] [position] ; selectint [z] [position 1] = selectint [z] [position] 1; Position ; addselect ( Selectchar, selectint, z, position, select);} else {if (position == 1) {return;} else {position ---; addselect;}}}} public void mycopy (String [] [] str1, string [] [] str2, int i, int j) {INTH; int K; for (h = 1; h <= i; h ) for (k = 1; k < = J; K ) STR2 [h] [k] = str1 [h] [k];} public void mycopy (string [] str1, string [] str2, int i) {int h; for (h = 1; h <= i; h ) str2 [h] = str1 [h];} public void mycopy (int [] str1, int [] str2, int i) {INTH;

For (h = 1; h <= i; h ) str2 [h] = str1 [h];}} Now we can use this JavaBean: Third, arrange so-called alignment in a JSP program, is taken from N objects All arrangements for m objects. In fact, we will make such a combination and then rinse each combination, and interested readers can try their own functions. (Author: everywhere)

转载请注明原文地址:https://www.9cbs.com/read-6421.html

New Post(0)