Java:数组中元素的成对组合
我有一个元素数组(偶数),我想成对地得到所有可能的元素组合 如果数组是Java:数组中元素的成对组合,java,arrays,combinations,Java,Arrays,Combinations,我有一个元素数组(偶数),我想成对地得到所有可能的元素组合 如果数组是 String[] test = new String[4]; String[] test= {"e1","e2","e3","e4"}; 输出应为: 组合1 e1-e2、e3-e4 组合2 e1-e3、e2-e4 组合3 e1-e4、e2-e3 组合4 e4-e1、e3-e2 组合5 e3-e1、e4-e2 组合6 e2-e1、e4-e3 为循环编写两个: for (int i =0; i < test.lengt
String[] test = new String[4];
String[] test= {"e1","e2","e3","e4"};
输出应为:
组合1 e1-e2、e3-e4
组合2 e1-e3、e2-e4
组合3 e1-e4、e2-e3
组合4 e4-e1、e3-e2
组合5 e3-e1、e4-e2
组合6 e2-e1、e4-e3
为循环编写两个:
for (int i =0; i < test.length; i++) {
for (int j = i + 1; j < test.length; j++) {
// code to print a[i] - a[j]
}
}
for(int i=0;i
这不是你在问题中提出的输出,但我相信这是你想要的输出,从你的运动队的评论来看
不要害怕循环-我试了一次就做到了,因为我认为它很容易抛出
我的想法是:找到所有的组合,就像我之前的答案一样。这是我开始的。接下来我做的事情(4个循环中的最后2个-k
和l
),是检查剩下的其他团队
因此,在所有元素中循环,检查i
和j
是否尚未使用,然后是k
。然后,再次循环遍历所有元素,检查是否尚未使用i
、j
和k
,然后这就是l
这应该适用于任何偶数个元素的数组(并且是通用的,因此也应该适用于字符串以外的其他对象):
试试这个:
String[] test= {"e1","e2","e3","e4"};
int count=1;
for(int i=0; i<test.length/2; i++)
for(int k=0, j=0; k<test.length; k++,j++) {
if(i==k) k++; if(j==i+test.length/2) j++;
System.out.print("Permutatation "+count+": ");
System.out.println(test[i]+"-"+test[k]+", "+test[i+test.length/2]+"-"+test[j]);
count++;
}
请显示您尝试了什么?如果您正在进行组合,则e1-e2
与e2-e1
的组合相同,并且应该只输出3个结果。如果您是在排列之后,那么它们是不同的,应该有24个结果输出(e1-e2,e3-e4
,e1-e2,e4-e3
,e2-e1,e3-e4
,e2-e1,e4-e3
,等等)。你想要排列或组合吗?我尝试过不同的“for”循环,但没有达到预期效果(我是初学者)。我在找排列显然,很抱歉误解了。输出应该像我在问题中写的那样。编辑(应该是12而不是24):如果你在排列之后,那么[…]应该有12个结果输出(e1-e2,e3-e4
,e1-e2,e4-e3
,e2-e1,e3-e4
,e2-e1,e4-e3
,等等)。谢谢,这与我尝试排除我的结果几乎相同=j检查。现在的问题是如何在没有元素重复的情况下将它们成对分组。我指的是排列组中元素的重复。像在输出中一样,排列1只能与排列5或排列8分组。这就像一个体育锦标赛,每组2个排列代表一个回合,每个队必须与另一个队比赛。输出必须显示所有回合。(每支球队在主客场各打一场,因此显然是排列而不是组合)@user3189770哦,好了,我现在明白了。我试试看,先生,你才是真正的MVP。非常感谢你
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.LinkedList;
public class Q2112634 {
public static class Pair<X> {
final X first;
final X second;
public Pair( final X one, final X two){
first = one;
second = two;
}
public String toString(){
return first.toString() + "-" + second.toString();
}
public String toReverseString(){
return second.toString() + "-" + first.toString();
}
}
@SuppressWarnings("unchecked")
public static <X> void getCombinations( final ArrayList<Pair<X>[]> combinations, final LinkedList<Pair<X>> pairs, final LinkedList<X> list ) {
// System.out.println(list.size());
final X first = list.removeFirst();
for ( int i = 0; i < list.size(); ++i ) {
final X second = list.remove( i );
final Pair<X> p = new Pair<X>( first, second );
pairs.addLast( p );
if ( list.size() == 0 )
{
combinations.add( pairs.toArray( (Pair[]) Array.newInstance( p.getClass(), pairs.size() ) ) );
}
else
{
getCombinations( combinations, pairs, list );
}
list.add( i, second );
pairs.removeLast();
}
list.addFirst( first );
}
static <E> String arrayToString( E[] arr ) {
if ( arr.length == 0 )
return "";
final StringBuffer str = new StringBuffer();
str.append( arr[0].toString() );
for ( int i = 1; i < arr.length; ++i )
{
str.append( ',' );
str.append( arr[i].toString() );
}
return str.toString();
}
public static void main(String[] args) {
String[] test = { "e1", "e2", "e3", "e4" };
int num_combinations = 1;
for ( int i = test.length - 1; i > 1; i = i - 2 )
num_combinations *= i;
final ArrayList<Pair<String>[]> combinations = new ArrayList<Pair<String>[]>( num_combinations );
final LinkedList<String> strings = new LinkedList<String>();
for ( String s : test )
strings.add( s );
getCombinations( combinations, new LinkedList<Pair<String>>(), strings );
System.out.println( "-----Combinations-----" );
int i = 1;
for ( Pair<String>[] combination: combinations ){
System.out.println( "Combination " + (i++) + " " + arrayToString( combination ) );
}
System.out.println( "-----Permutations-----" );
i = 1;
for ( Pair<String>[] combination: combinations ){
for ( int j = 0; j < Math.pow( 2, combination.length ); ++j )
{
System.out.print( "Permutation " + (i++) + " " );
for ( int k = 0; k < combination.length; ++k )
{
if ( k > 0 )
System.out.print(',');
if ( (j & ( (int) Math.pow( 2, k ) ) ) == 0 )
System.out.print( combination[k].toString() );
else
System.out.print( combination[k].toReverseString() );
}
System.out.println();
}
}
}
}
-----Combinations-----
Combination 1 e1-e2,e3-e4
Combination 2 e1-e3,e2-e4
Combination 3 e1-e4,e2-e3
-----Permutations-----
Permutation 1 e1-e2,e3-e4
Permutation 2 e2-e1,e3-e4
Permutation 3 e1-e2,e4-e3
Permutation 4 e2-e1,e4-e3
Permutation 5 e1-e3,e2-e4
Permutation 6 e3-e1,e2-e4
Permutation 7 e1-e3,e4-e2
Permutation 8 e3-e1,e4-e2
Permutation 9 e1-e4,e2-e3
Permutation 10 e4-e1,e2-e3
Permutation 11 e1-e4,e3-e2
Permutation 12 e4-e1,e3-e2
String[] test= {"e1","e2","e3","e4"};
int count=1;
for(int i=0; i<test.length/2; i++)
for(int k=0, j=0; k<test.length; k++,j++) {
if(i==k) k++; if(j==i+test.length/2) j++;
System.out.print("Permutatation "+count+": ");
System.out.println(test[i]+"-"+test[k]+", "+test[i+test.length/2]+"-"+test[j]);
count++;
}
Permutatation 1: e1-e2, e3-e1
Permutatation 2: e1-e3, e3-e2
Permutatation 3: e1-e4, e3-e4
Permutatation 4: e2-e1, e4-e1
Permutatation 5: e2-e3, e4-e2
Permutatation 6: e2-e4, e4-e3