我有一系列元素(偶数),我想在夫妻中获得所有可能的组合。
如果数组是
String[] test = new String[4];
String[] test= {"e1","e2","e3","e4"};
输出应为:
Combination1 e1-e2 , e3-e4
Combination2 e1-e3 , e2-e4
Combination3 e1-e4 , e2-e3
Combination4 e4-e1 , e3-e2
Combination5 e3-e1 , e4-e2
Combination6 e2-e1 , e4-e3
答案 0 :(得分:1)
写两个for
循环:
for (int i =0; i < test.length; i++) {
for (int j = i + 1; j < test.length; j++) {
// code to print a[i] - a[j]
}
}
答案 1 :(得分:1)
String[] test = {"e1","e2","e3","e4"};
for (int i = 0; i < test.length; i++) {
for (int j = i + 1; j < test.length; j++) {
System.out.print(test[i] + " - " + test[j]);
boolean foundExtra = false;
for (int k = 0; k < test.length && !foundExtra; k++)
{
if (k != j && k != i)
{
for (int l = 0; l < test.length; l++)
{
if (l != k && l != j && l != i)
{
System.out.println(" , " + test[k] + " - " + test[l]);
foundExtra = true;
break;
}
}
}
}
}
}
会给出输出:
e1 - e2 , e3 - e4
e1 - e3 , e2 - e4
e1 - e4 , e2 - e3
e2 - e3 , e1 - e4
e2 - e4 , e1 - e3
e3 - e4 , e1 - e2
这不是你在问题中输出的输出,但我相信这是你想要的输出,从运动队评论来判断。
不要害怕循环 - 我一次尝试这样做是因为我觉得很容易丢掉它。
我在想什么:找到所有组合,就像我之前的回答一样。这就是我的开始。我做的下一件事(4个循环中的最后2个 - k
和l
)检查了剩下的其他团队。
因此,循环浏览所有元素,检查i
和j
是否尚未使用,然后是k
。然后,再次遍历所有元素,检查i
,j
和k
是否尚未使用,那是l
。
答案 2 :(得分:0)
这适用于任意偶数个元素的数组(并且是通用的,因此除了字符串之外还应该用于其他内容):
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
答案 3 :(得分:0)
试试这个:
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