Home > Back-end > For bosses to solve thank you! Two-dimensional array algorithm
For bosses to solve thank you! Two-dimensional array algorithm
Time:12-04
The existing two-dimensional array String [] [] arr3=new String [] [] {{" A ", "B"}, {" C ", "D", "G"}, {" E ", "F"}}; How to output the ACE, ADE, AGE, ACF, ADF, AGF, BCE, BDE, BGE, BCF, BDF, BGF (the length of the array is not fixed, small array length is not fixed)
CodePudding user response:
The permutation and combination problem, too many answered Write to you, for example, understand yourself
public class Sample { Public static void main (String [] args) { Arr3=new String String [] [] [] [] {{" A ", "B"}, {" C ", "D", "G"}, {" E ", "F"}}; Int [] independence idx=new int [arr3 length]. for (int i=0; iWhile (independence idx [0] for (int i=0; iSystem. The out. Printf (" % s ", arr3 [I] [independence idx [I]]). } System.out.println(); Independence idx [independence idx. Length - 1] + +; For (int I=independence idx. Length - 1; I> 0; I -) { If (independence idx [I] & gt;=arr3 [I]. Length) { Independence idx [I]=0; Independence idx [I - 1] + +; } } } } }
If you don't understand, you can refer to the following post ideas that carry (simulation) https://bbs.csdn.net/topics/397288252
CodePudding user response:
With the result of your overdue order, ok, permutation and combination algorithm,
private static List//idea is recursive spell before the first two arrays, and then finish spell as a character array with the rest of the array to form a new two-dimensional arrays, recursive calls again, If (arr. The size () & lt;=1) { Return arr. } else { String [] stringlist1=arr. Get (0); String [] stringlist2=arr. Get (1); String [] strings=new String [stringlist1. Length * stringlist2. Length]. for (int i=0; I & lt; Stringlist1. Length; I++) { For (int j=0; J & lt; Stringlist2. Length; J++) { Strings [j + stringlist2 length * I]=stringlist1 stringlist2 [I] + [j]; } } ListAnotherArr. Add (strings); for(int i=2; iAnotherArr. Add (arr. Get (I)); } Return makeStr (anotherArr); } }