如题,有数组a,例如:a={ "1","3","5","6","8"};
数组b,例如:b={ "2","7","9","10","11"};
从数组a中,随机选取2个数,总共有10种排列组合
为:1 3, 1 5,1 6, 1 8, 3 5,
3 6, 3 8, 5 6, 5 8, 6 8
从数组b中,随机选取3个数,总共ye有10种排列组合
为:2 7 9, 2 7 10, 2 7 11 , 7 9 10, 7 9 11,
7 10 11, 9 10 11, 2 9 10, 2 9 11, 2 10 11
现结果想得到 a组合和 b组合的组合,例如 13279,132710,132711,.....,682911,6821011共100种的输出结果的算法,求高人指点,谢谢!
数组b,例如:b={ "2","7","9","10","11"};
从数组a中,随机选取2个数,总共有10种排列组合
为:1 3, 1 5,1 6, 1 8, 3 5,
3 6, 3 8, 5 6, 5 8, 6 8
从数组b中,随机选取3个数,总共ye有10种排列组合
为:2 7 9, 2 7 10, 2 7 11 , 7 9 10, 7 9 11,
7 10 11, 9 10 11, 2 9 10, 2 9 11, 2 10 11
现结果想得到 a组合和 b组合的组合,例如 13279,132710,132711,.....,682911,6821011共100种的输出结果的算法,求高人指点,谢谢!
String[] a = {"1","3","5","6","8"};
String[] b = {"2","7","9","10","11"}; List<String> list = new ArrayList<String>(); int len_a = a.length,len_b = b.length;
for(int i = 0;i < len_a;i++){
for(int j = i+1;j < len_a;j++){
for(int k =0; k < len_b;k++){
for(int x = k+1;x < len_b;x++){
list.add(a[i]+""+a[j]+""+b[k]+""+b[x]);
}
}
}
}
System.out.println(list.size());
System.out.println(list.toString());
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;enum Flag
{
ARRAYa, ARRAYb;
}public class Test
{
static ArrayList<String> array;
static List<String> list = new ArrayList<String>();
static ArrayList<String> array1 = new ArrayList<String>();
static ArrayList<String> array2 = new ArrayList<String>();
static ArrayList<String> array3 = new ArrayList<String>(); public static void main(String[] args)
{
String[] a = { "1", "3", "5", "6", "8" }; String[] b = { "2", "7", "9", "10", "11" }; new Test().convert(a, array1, Flag.ARRAYa); new Test().convert(b, array2, Flag.ARRAYb); result();
} public static void result()
{
for (int i = 0; i < 10; i++)
{
for (int q = 0; q < 10; q++)
{
array3.add(array1.get(i) + array2.get(q));
}
}
int count = 0;
for (String s : array3)
{
System.out.print(s + " ");
count++;
if (0 == count % 10)
{
System.out.println();
}
}
} public void convert(String[] s, ArrayList<String> a, Enum e)
{
list = Arrays.asList(s); array = new ArrayList<String>(list);
if (Flag.ARRAYa == e)
{
for (int i = 0; i < 4; i++)
{
String temp = array.remove(0); for (int k = 0; k < array.size(); k++)
{
a.add(temp + array.get(k)); } }
} if (Flag.ARRAYb == e)
{
String temp = array.remove(0); for (int i = 0; i < 2; i++)
{ if (1 == i)
{
array = new ArrayList<String>(list);
array.remove(0);
temp = array.remove(0);
} for (int k = 0; k < 3; k++)
{
String temp1 = array.remove(0);
String temp2 = temp + temp1;
for (int q = 0; q < array.size(); q++)
a.add(temp2 + array.get(q));
}
if (i == 1)
{
a.add("91011");
} }
} }}
用回溯算法就可以了 public static void printCom(int[] nums, int count) {// nums为输入可选数集合,count输出其中几个
innerPriCom(nums, count, 0, count);
} private static void innerPriCom(int[] nums, int count, int foIndex,
int lgIndex) {
if (foIndex == count) {
for (int i = 0; i < count; i++) {
System.out.print(nums[i] + " ");
}
System.out.println();
return;
}
innerPriCom(nums, count, foIndex + 1, lgIndex);
for (int i = lgIndex; i < nums.length; i++) {
nums[foIndex] ^= nums[i];
nums[i] ^= nums[foIndex];
nums[foIndex] ^= nums[i];
innerPriCom(nums, count, foIndex + 1, i + 1);
nums[foIndex] ^= nums[i];
nums[i] ^= nums[foIndex];
nums[foIndex] ^= nums[i];
}
}
这个是得到一个集合的某几个元素的各种组合