such as:
* A ; B C D
1 0.2 ; 0.3 0.4 0.5
2 0.3 ; 0.4 0.2 0.5
3 0.5 ; 0.2 0.9 0.1
........ ...............
such a two-dimensional array , the array you want to loop through all the combinations , just for example:
AB, AC, AD, BC, BD, CD
ABC, ABD, BCD
ABCD
then the size of a two-dimensional array is uncertain.
traversal process : Currently A B, then vertically through all the data underneath A B column.
great God who ask you , what is the best way?
current thinking :
1. Firstly because the array size is uncertain , for loop is certainly not
2. Secondly, I tried various combinations of methods and found that various combinations of the corresponding algorithms for each combination of input , which is a combination of the results of the second line is not correspond with the first line of
* Currently no code, would like to clarify some ideas , find a good solution for a long time did not find
Thank you in advance :)
------ Solution ------------------------------------ --------
public class Combination {
public static void main(String[] args){
String[] arr={"A","B","C","D","E"};
String[][] arrTD=getTwoDimensionArray(arr);
/*for(int i=0;i<getCombination(arr,3).length;i++)
System.out.print(" "+getCombination(arr,3)[i]);
*/
for(int i=0;i<arrTD.length;i++){
for(int j=0;j<arrTD[i].length;j++){
System.out.print(" "+arrTD[i][j]);
}
System.out.println();
}
}
public static String[][] getTwoDimensionArray(String[] arr) {
/**将getCombination所有一维数组赋值给新数组*/
String[][] newarr=new String[arr.length][];
for(int n=1;n<=arr.length;n++){
newarr[n-1]=getCombination(arr,n);
}
return newarr;
}
public static String[] getCombination(String[] arr, int n){
//这里还要检测n必须不超过arr.length,这里就省略了
/**从一个一维数组中取得n个元素所构成的组合,并视AB BA为相同组合*/
//这里采取类似选择排序的for循环方式
//这里还要采用递归的算法getCombination(n,arr)要利用getCombination(n-1,arr);n>=2;
String[] newarr=new String[getLength(arr,n)];
//System.out.println(n+" n "+newarr.length);
//System.out.println((n-1)+" n-1 "+newarrminus1.length);
//int currentIndex=0;
int count=0;
if(n==1){
for(int i=0;i<arr.length;i++){
newarr[i]=arr[i];
}
}
if(n>=2){
for(int j=0;j<arr.length-1;j++){
String currentValue=arr[j];
int len=newarr.length;
int lensub=subArray(arr,j+1).length;
int lengetCombinationsub=getCombination(subArray(arr,j+1),n-1).length;
/**for(int i=0;i<len;i++){*/
for(int k=0;k<lengetCombinationsub;k++){
//System.out.println(n+" +"+count);
//System.out.print(j+" "+getCombination(subArray(arr,j+1),n-1)[k]+" ");
//这里出现容易一个错误,getCombination(subArray(arr,j+1),n-1)的长度问题
if(lensub>=n-1){
newarr[count]=currentValue+
getCombination(subArray(arr,j+1),n-1)[k];
}
count++;
}
}//for 1
}
return newarr;
//StringBuilder ss=new StringBuilder();
}
public static int getLength(String[] arr,int n){
//计算从一个一维数组中取得n个元素所构成的组合的个数
int len =arr.length;
/*if(n==2)
System.out.println(n+" arr.len "+len);*/
int getlen =0;
int fenzi =len;
int fenmu =1;
for(int i=1;i<n;i++){
int temp =len;
fenzi=fenzi*(temp-i);
/*if(n==2)
System.out.println(n+" fenzi "+fenzi+" "+len);*/
int demp=1;
fenmu=fenmu*(demp+i);
}
getlen=fenzi/fenmu;
//System.out.println(getlen);
return getlen;
}
public static String[] subArray(String[] arr ,int n){
/**分割一个字符串数组,获得一个新的字符串数组从n位到len-1位*/
int len=arr.length;
String[] newarr=new String[len-n];
System.arraycopy(arr, n, newarr, 0, len-n);
//System.out.println(newarr[0]+" "+newarr[len-n-1]+" "+(len-n));
return newarr;
}
}
Anyway I knocked out the code for the conditions you say , I will re- times
because it involves a recursive method , debug for a long time , and only then the program to knock them out.
------ For reference only -------------------------------------- -
this is a combination of mathematical issues now .
AB, AC, AD, BC, BD, CD not Cn2. You can use
for(int i =0;i< length;i++){
for(int j =i+1;i< length;i++){
..
}
}
completed , it should be similar to other combinations
------ For reference only --------------------------- ------------
wrong
for(int i =0;i< length;i++){
for(int j =i+1;j< length;j++){
..
}
}
------ For reference only ----------------------------------- ----
know why
------ For reference only ---------------------------------------
Thank you for your answer, but I am here because there is a two-dimensional array of many different sizes , this basic for loop I have thought of , but because each two-dimensional array of sizes , these combinations some are taking to 4 , some are taking to six , or take other number to another , simply use the for loop I felt like it working .
------ For reference only -------------------------------------- -
Alas, this problem has troubled me for several days
------ For reference only ------------------------ ---------------
first organization about language , clearly describe the problem ah , read 4 times, do not know what the problem is in the end the landlord
------ For reference only ---------------------------------------
Sorry, I could describe very clearly wasting your time .
fact is this, I would now like to find a way to traverse all combinations of two-dimensional array , such as the example I described in the first floor of the same,
* A ; B C D
1 0.2 ; 0.3 0.4 0.5
2 0.3 ; 0.4 0.2 0.5
3 0.5 ; 0.2 0.9 0.1
........ ...............
(1). This is a two-dimensional array , it can be said that a table , the table size is different , that is not all table columns (columns) are only ABCD, there may be ABC, ABCDE, ABCDEF other possible and much more Number ( 2 ) . rows (rows) of
is uncertain
(3). intermediate data, such as the above example , A-1 0.2 A description of the subject 1 is completed by the time the 0.2 seconds , so other
(4). My hand has a standard , such as the standard is 0.3 , higher than 0.3 on the failure , all are less than 0.3 when it takes a minimum of that , I now need to traverse the two-dimensional All combinations of the array to see which combinations up to meet the standards (that is, and set the maximum ) . For example, AB 0.3 standard , the 100 questions total solution of the 80 questions , AC 0.3 standard , the 100 questions total solution of the 90 questions , ABC at 0.3 standard, a total of 100 questions , 96 questions solution , thereby analogy
my language poor organizational skills , inconvenience caused trouble understanding it, there is not clear , I will continue to add
------ For reference only ---------- -----------------------------
Sorry, I could describe very clearly wasting your time .
fact is this, I would now like to find a way to traverse all combinations of two-dimensional array , such as the example I described in the first floor of the same,
* A ; B C D
1 0.2 ; 0.3 0.4 0.5
2 0.3 ; 0.4 0.2 0.5
3 0.5 ; 0.2 0.9 0.1
........ ...............
(1). This is a two-dimensional array , it can be said that a table , the table size is different , that is not all table columns (columns) are only ABCD, there may be ABC, ABCDE, ABCDEF other possible and much more Number ( 2 ) . rows (rows) of
is uncertain
(3). intermediate data, such as the above example , A-1 0.2 A description of the subject completed the 1st time by 0.2 seconds , and so on other
(4). My hand has a standard , such as the standard is 0.3 , higher than 0.3 on the failure , all are less than 0.3 when it takes a minimum of that , I now need to traverse the two-dimensional All combinations of the array to see which combinations up to meet the standards (that is, and set the maximum ) . For example, AB 0.3 standard , the 100 questions total solution of the 80 questions , AC 0.3 standard , the 100 questions total solution of the 90 questions , ABC 0.3 standard , the 100 questions , 96 questions total solution , thus analogy
my language poor organizational skills , inconvenience trouble understanding it, there is no clear I will continue to add
" The size of this table is different " and do not understand ? Did you traverse it, still not sure? , You traverse the two-dimensional array , when the number of rows, each row number should be determined right
------ For reference only ------------ ---------------------------
dynamic application direct memory ? Will ?
then maintain the memory address list , think how dry how dry
------ For reference only --------------------- ------------------
Sorry, I could describe very clearly wasting your time .
fact is this, I would now like to find a way to traverse all combinations of two-dimensional array , such as the example I described in the first floor of the same,
* A ; B C D
1 0.2 ; 0.3 0.4 0.5
2 0.3 ; 0.4 0.2 0.5
3 0.5 ; 0.2 0.9 0.1
........ ...............
(1). This is a two-dimensional array , it can be said that a table , the table size is different , that is not all table columns (columns) are only ABCD, there may be ABC, ABCDE, ABCDEF other possible and much more Number ( 2 ) . rows (rows) of
is uncertain
(3). intermediate data, such as the above example , A-1 0.2 A description of the subject 1 is completed by the time the 0.2 seconds , so other
(4). My hand has a standard , such as the standard is 0.3 , higher than 0.3 on the failure , all are less than 0.3 when it takes a minimum of that , I now need to traverse the two-dimensional All combinations of the array to see which combinations up to meet the standards (that is, and set the maximum ) . For example, AB 0.3 standard , the 100 questions total solution of the 80 questions , AC 0.3 standard , the 100 questions total solution of the 90 questions , ABC 0.3 standard , the 100 questions , 96 questions total solution , thus analogy
my language poor organizational skills , inconvenience trouble understanding it, there is no clear I will continue to add
" The size of this table is different " and do not understand ? Did you traverse it, still not sure? , You traverse the two-dimensional array , when the number of rows, each row number , it should be determined in
when traversing a long table size and everything is able to determine , but it seems like the answer above, buddy , if you want to use for loop, then the number of the for loop is based on the size of the table and change , for example , when a combination of the two is to use two for loops, three is a time to use a combination of the three for loops.
such as:
AB, AC, AD .... ; ---> for (.....)
for (....)
ABC, ABD ..... - -> for (.....)
for (....)
for (....)
As I spoke above , the table size is different , and some may take AB, AC and combination of both ok, some may want to take the other combinations ABCDEF , etc., etc. ( depending on the length of the table to determine ) , I mainly want to solve combinatorial problems , I do not know how you can order not to repeat the traverse all combinations. Clearly did not know the description , thank you for your reply :)
------ For reference only ---------------- -----------------------
c / c + + under the will , under java do not understand, but said not thought through how to dynamic memory applications applied to solve the problem on this combination , look great God enlighten me :)
------ For reference only ---------------------- -----------------
so you can traverse out? have not tried it myself look at the landlord 's title , your outer loop Yes, the inner loop will not work
------ For reference only ---------------------------------------
so you can traverse out? have not tried it myself look at the landlord 's title , your outer loop Yes, the inner loop will not work
Thank you reply , probably because my description is not clear enough to understand it so wrong upstairs might
------ For reference only --------------- ------------------------
c / c + + under the will , under java do not understand, but said not thought through how dynamically allocate memory applied to solve the problem on this combination , look great God enlighten me :)
byte size = 1;
long allocateMemory = UNSAFE.allocateMemory(size);
UNSAFE.putAddress(allocateMemory, value);
long readValue = UNSAFE.getAddress(allocateMemory);
I am not a great God, which is the way java application memory . Baidu unsafe
------ For reference only --------------------------------- ------
passing learn,
------ For reference only ---------------------------------------
c / c + + underneath will , under java do not understand, but said not thought through how to dynamically allocate memory on this combination is applied to solve the problem , look great God enlighten me :)
byte size = 1;
long allocateMemory = UNSAFE.allocateMemory(size);
UNSAFE.putAddress(allocateMemory, value);
long readValue = UNSAFE.getAddress(allocateMemory);
I am not a great God, which is the way java application memory . Baidu unsafe
Thank you reply , can you tell me how to use this method on my problem? Thank you for helping me find information that I could not straighten out mainly on ideas, please tell what ? Thank you :)
------ For reference only ----------------------------------- ----
landlord to do a lot of explanation on the subject and supplements , I have Mo Kanwan , sorry. But I'll give myself the subject of the original landlord answer .
AB, AC, AD, BC, BD, CD
ABC, ABD, BCD
ABCD
These should be targeted is a combination of a one-dimensional array , if coupled with A, B, C, D, ie, the array {A, B, C, D} of all combinations .
here only if the landlord wants to get all of these combinations ( a String output ) , rather than the nature of the results of arithmetic multiplication hope to draw the following code to help you, absolutely original.
I do not know here on the assumption that the length of this one-dimensional array , and get all the combinations ( removal of A, B, C, D) to a two-dimensional array of nature preservation portfolio
i.e. , {{AB, AC, AD, BC, BD, CD}, {ABC, ABD, BCD}, {ABCD}} This two-dimensional array , I Forum newcomers , like him help.
Today, time is running out , but also to break the code first idea to the landlord to knock out look .
public class Combination {
public static void main (String [] args) {
String [] arr = {"A", "B", "C", "D"};
getTwoDimensionArray (arr);
}
public static String [] [] getTwoDimensionArray (String [] arr) {
}
public static String [] getCombination (String [] arr, int n) {
/ ** get a combination consisting of n elements from a one-dimensional array * /
/ / Select the sort of place to take a similar approach for loop
/ / here also uses a recursive algorithm getCombination (n, arr) to take advantage of getCombination (n-1, arr); n> = 2;
/ * if (n> = 2) {
* for (int j = 0;. j
}
}
}
if (n = 1) {
for (int i = 0; i
}} * /
/ / where arr To call arr.substring (n-1, length);
String CurrentValue;
int CurrentIndex;
/ / StringBuilder ss = new StringBuilder ();
}
/ * public static int getLength (String [] arr, int n) {
/ / calculate combinations achieved composed of n elements from a number of one-dimensional array
* This method exists necessity Unknown
} * /
}
------ For reference only --------------------------------- ------
/ / code retransmission look
public class Combination {
public static void main (String [] args) {
String [] arr = {"A", "B", "C", "D"};
getTwoDimensionArray (arr);
}
public static String [] [] getTwoDimensionArray (String [] arr) {
}
public static String [] getCombination (String [] arr, int n) {
/ ** get a combination consisting of n elements from a one-dimensional array * /
/ / Select the sort of place to take a similar approach for loop
/ / here also uses a recursive algorithm getCombination (n, arr) to take advantage of getCombination (n-1, arr); n> = 2;
/ * if (n> = 2) {
* for (int j = 0;. j
}
}
}
if (n = 1) {
for (int i = 0; i
}} * /
/ / where arr To call arr.substring (n-1, length);
String CurrentValue;
int CurrentIndex;
/ / StringBuilder ss = new StringBuilder ();
}
/ * public static int getLength (String [] arr, int n) {
/ / calculate combinations achieved composed of n elements from a number of one-dimensional array
* This method exists necessity Unknown
} * /
}
------ For reference only --------------------------------- ------
public class Combination {
//发帖还不熟悉,所以再重发一下。
public static void main(String[] args){
String[] arr={"A","B","C","D"};
getTwoDimensionArray(arr);
}
public static String[][] getTwoDimensionArray(String[] arr) {
}
public static String[] getCombination(String[] arr, int n){
/**从一个一维数组中取得n个元素所构成的组合*/
//这里采取类似选择排序的for循环方式
//这里还要采用递归的算法getCombination(n,arr)要利用getCombination(n-1,arr);n>=2;
/*if(n>=2){
* for(int j=0;j<getCombination(n,arr).length;j++){
for(int i=0;i<getCombination(n-1,arr).length;i++){
getCombination(n,arr)[j]=CurrentValue+getCombination(n-1,arr)[i];}
}
}
}
if(n=1){
for(int i=0;i<arr.length;i++){
getCombination(1,arr)[i]=arr[i];
}}*/
//其中arr要调用arr.substring(n-1,length);
String CurrentValue;
int CurrentIndex;
//StringBuilder ss=new StringBuilder();
}
/*public static int getLength(String[] arr,int n){
//计算从一个一维数组中取得n个元素所构成的组合的个数
* 这个方法存在必要性未知
}*/
}
------ For reference only -------------------------- -------------
Thank you ! Thank you very much to spend time thinking about my problem and write code to me. I tried this method , and I want to reach the target seems unlikely match. I am essentially not just want to arrange a combination of ABCD , but I want according to their order, such as it is AB, then longitudinal data on AB than two columns below, then such is now the BCD, I would need more than BCD data for these three columns below .
------ For reference only -------------------------------------- -
------ For reference only ---------------------------------- -----
Thank you ! Thank you very much to spend time thinking about my problem and write code to me. I tried this method , and I want to reach the target seems unlikely match. I am essentially not just want to arrange a combination of ABCD , but I want according to their order, such as it is AB, then longitudinal data on AB than two columns below, then such is now the BCD, I would need more than BCD data for these three columns below .
looked at your needs, consider cross- linked list implementation
------ For reference only ----------------------- ----------------
assumed that for the field A, B, C, D, E, so that E = 2 ^ 0, D = 2 ^ 1, C = 2 ^ 2, B = 2 ^ 3, A = 2 ^ 4, a loop directly
for (i = 1; i <2 ^ 5; i + +) {
/ / find the binary representation of i , if the corresponding bit is 1 , then the corresponding column into the collection, and then stitching
}
This can be a
------ For reference only ------------------------------ ---------
I essentially not just want to arrange a combination of ABCD , but I want according to their order, such as it is AB, and AB compare two columns below the longitudinal data , and then for example, now is the BCD, BCD I need to compare these three data below .
* A ; B C D
1 0.2 ; 0.3 0.4 0.5
2 0.3 ; 0.4 0.2 0.5
3 0.5 ; 0.2 0.9 0.1
Please state the specific comparison method , how the three columns under the BCD data comparison means comparing the square of the maximum value and the minimum value is relatively quite B1, C2, D3 , or other types of mathematical comparison mode , the comparison mode of the use of these not the same as the class , not the same idea knock code .
but as long as they have all been calculated arrangement , calculated according to the length arrangement , each subscript characters , these problems are solvable , that is, when read into the data , you need to charge some effort .
There I am learning java, too deep knowledge do not know, but for the most part on javaSE still very familiar .
------ For reference only ---------------------------------- -----
personal opinion , the landlord said you can not loop through the use for, it is because too many possibilities arranged these possibilities must be composed of an array of corresponding and then unified traversal.
first count ABCDE etc. list all possible permutations possible , these may form a two-dimensional array , these two-dimensional array into an array , then probably all traverse out then you want to pick your condition data .
------ For reference only -------------------------------------- -
Here is how the possibility of loading the code , the rest of you get it
public class test {
static double [] a = {0.1,0.4,0.5};
static double [] b = {0.3,0.2,0.6};
static double [] c = {0.4,0.1,0.4};
static double [] d = {0.7,0.6,0.2};
static double [] f = {0.3,0.5,0.2};
static double [] [] arry = {a, b, c, d, f};
public static void main (String [] args) {
method (arry);
}
public static void method (double [] [] arr) {
/ / loaded rows as possible
List
for (int i = 0; i
newarr [0] = arr [i];
newarr [1] = arr [j];
ar.add (newarr);
for (int k = j +1; k
newarr1 [0] = arr [i];
newarr1 [1] = arr [j];
newarr1 [2] = arr [k];
ar.add (newarr1);
for (int x = k +1; x
newarr2 [0] = arr [i];
newarr2 [1] = arr [j];
newarr2 [2] = arr [k];
newarr2 [3] = arr [x];
ar.add (newarr2);
for (int l = x +1; l
newarr3 [0] = arr [i];
newarr3 [1] = arr [j];
newarr3 [2] = arr [k];
newarr3 [3] = arr [x];
newarr3 [4] = arr [l];
System.out.println (Arrays.toString (arr [l]));
ar.add (newarr3);
}
}
}
}
}
for (int i = 0; i
for (int j = 0; j
System.out.print (Arrays.toString (temp [j]) + "");
}
}
System.out.println ();
}
}
}
------ For reference only --------------------------------- ------
Thank you ! Thank you very much to spend time thinking about my problem and write code to me. I tried this method , and I want to reach the target seems unlikely match. I am essentially not just want to arrange a combination of ABCD , but I want according to their order, such as it is AB, then longitudinal data on AB than two columns below, then such is now the BCD, I would need more than BCD data for these three columns below .
looked at your needs, consider the realization of cross- linked
Yes, I went to learn about relevant information, thank you
------ For reference only ----------------------- ----------------
Thank you, binary way I try
------ For reference only ------------------------ ---------------
very grateful ! Too hard on you! You're right , I did not describe well, my idea is :
I have a standard on hand , such as correspondence at this table, I have a standard 0.4 , the data of these three columns under BCD, is B1, C1, D1 such a row of data with a standard ( in this example : 0.4 ) compared to the three and then if more than 0.4, do not do anything, if only a less than 0.4 , then this problem be that players win if all below 0.4 , the lowest score ( in fact, the score here is time, it is the fastest man to win ) , and finally to calculate the total number of questions to get
From another point of view :
If each column are regarded as a collection of my goal is to calculate the different collections and sets.
------ For reference only -------------------------------------- -
------ For reference only ---------------------------------- -----
LZ mean that all buttoned want to find a topic or several players , and lists the winners of those topics ?
If this is the case , re- direct a two cycle can get, do not have to ask all of the union , this is the time difference between n * n and 2 ^ n between ah
------ For reference only ---------------------------------- -----
yesterday too tired , too late to see the landlord replies , so no posts. I can re- try according to the landlord , but a friend said the replacement would be simple binary
Assuming the field A, B, C, D, E, so that E = 2 ^ 0, D = 2 ^ 1, C = 2 ^ 2, B = 2 ^ 3, A = 2 ^ 4, a loop directly
for (i = 1; i <2 ^ 5; i + +) {
/ / find the binary representation of i , if the corresponding bit is 1 , then the corresponding column into the collection, and then stitching
}
night I thought better of the drawbacks of this idea in the next : .
question 1 "E" = 2 ^ 0, this process, we want to keep the 2 ^ 0 , and the need to retain "E", which is the need to re- apply for a variable , and add a method , as follows :
String E="E";Of course, when you actually go
int e=2^0;
int method(String E){
if(E.equals("E))}return 2^0;
write this program , we need to do a lot for loop, we can easily know , do not write complete.
Question 2 . / / find the binary representation of i , if the corresponding bit is 1 , then the corresponding column into the collection, and then stitching , really nice idea , and I can not admit that this is a fast get all sorting. . Requires only two ways: 1 the decimal to binary conversion method (java in providing ) ; 2 corresponds to a binary string obtained .
However, this method is not given defect
AB, AC, AD, BC, BD, CD
ABC, ABD, BCD
ABCD like a regular sort , to achieve this sort of regular binary method is cumbersome .
months I view it, just say it to my own understanding .
------ For reference only -------------------------------------- -
but my standards are based on a combination of becoming , is calculated , not always constant
------ For reference only --------- ------------------------------
very grateful !
= .
------ For reference only -------------------------------------- -
.
没有评论:
发表评论