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);
}
}

CodePudding user response:

reference 1st floor qybao 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
thank??

CodePudding user response:

refer to the second floor lonely sea response:
does not agree 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);
}
}
thank??

CodePudding user response:

Professional clumsily
 
Private static String [] getArr (String [] arr1, String [] arr2) {
String [] STRS=new String [arr1. Length * arr2. Length].
Int index=0;
for (int i=0; I & lt; Arr1. Length; I++) {
String STR="";
For (int j=0; J & lt; Arr2. Length; J++) {
STR=arr1 [I];
STR +=arr2 [j];
STRS [index++]=STR;
}
}
Return the STRS;
}
Public static String [] func (String [] [] arr) {
If (arr==null) {
return null;
}
If (arr. Length<=1) {
Return arr [0].
}
For (int I=1; I & lt; Arr. Length; I++) {
String [] s=new String [arr [I - 1]. Length * arr [I] length].
S=getArr (arr] [I - 1, arr [I]);
Arr [I]=s;
}
Return arr (arr. Length - 1),
}

CodePudding user response:

 
Arr3=new String String [] [] [] [] {{" A ", "B"}, {" C ", "D", "G"}, {" E ", "F"}};
Int [] independence idx=new int [arr3 length].
int i=0;
While (independence idx [arr3. Length - 1] for (i=0; iSystem. The out. Print (arr3 [I] [independence idx [I]]).
I=0;
System.out.println();
Independence idx [I] + +;
While (iIndependence idx [I]=0;
Independence idx/+ + I + +;
}

}
null
  • Related