Java数组分成N个数组的所有组合
假设一个数组有M个元素,要将该数组分成N个数组,求所有组合。用Java实现,谢谢了如M=3,N=2时,假设数组a={a,b,c},想要结果为:{a}{b,c}、{a,b}...
假设一个数组有M个元素,要将该数组分成N个数组,求所有组合。
用Java实现,谢谢了
如M=3,N=2时,假设数组a={a,b,c},想要结果为:{a}{b,c} 、{a,b}{c}、{a,c}{b}
想出来了,就是高中数学排列组合分组隔板法。 展开
用Java实现,谢谢了
如M=3,N=2时,假设数组a={a,b,c},想要结果为:{a}{b,c} 、{a,b}{c}、{a,c}{b}
想出来了,就是高中数学排列组合分组隔板法。 展开
4个回答
展开全部
这个问题不是这么想的,
你可以想象一个n位二进制的数,找出所有只有k个1,其他位都是0的数,这个二进制数的第x位为1就表示取字母表中的第x个字母,为0不取,最后得到的就是这个二进制数代表的组合,将所有的二进制数都翻译成字母组合后,就是你要取得的所有字母组合了。
如果实在不会的话,待会再给你写个代码
public class Combination {
public static void main(String[] args) {
String[] valueSets = { "a", "b", "c", "d", "e" };
int n = 3;
List<String> list = combination(valueSets, n);
System.out.println(list);
for(String string: list){
System.out.println(string);
}
System.out.println("一共 "+list.size()+" 个。");
}
public static List<String> combination(String[] valueSets, int n) {
System.out.println(">>>>>combination");
List<String> binaryList = searchBinaryList(valueSets.length, n);
List<String> combinationList = toCombinationList(binaryList, valueSets);
return combinationList;
}
public static List<String> toCombinationList(List<String> binaryList,
String[] valueSets) {
List<String> combinationList = new ArrayList<String>();
for (String binary : binaryList) {
String combination = changeBinaryToCombination(binary, valueSets);
if (combination != null && combination.trim() != "") {
combinationList.add(combination);
}
}
return combinationList;
}
public static String changeBinaryToCombination(String binary,
String[] valueSets) {
String combination = "";
if (binary == null || binary.trim() == "") {
return null;
}
for (int i = 0; i < binary.length(); i++) {
if (binary.charAt(i) == '1') {
combination += valueSets[i];
}
}
return combination;
}
public static List<String> searchBinaryList(int length, int n) {
System.out.println(">>>>>searchBinaryList");
List<String> binaryList = new ArrayList<String>();
for (int i = 0; i < (int) Math.pow(2, length); i++) {
String binary = Integer.toBinaryString(i);
int count = oneCountsContainsInBinary(binary);
if (count == n) {
binaryList.add(toSpecifiedBitsBinary(binary, length));
}
}
return binaryList;
}
public static String toSpecifiedBitsBinary(String binary, int length) {
String specifiedBitsBinary = "";
for (int i = 0; i < length - binary.length(); i++) {
specifiedBitsBinary += 0;
}
specifiedBitsBinary += binary;
return specifiedBitsBinary;
}
public static int oneCountsContainsInBinary(String binary) {
int count = 0;
if (binary == null || binary.trim() == "") {
return count;
}
for (int i = 0; i < binary.length(); i++) {
if (binary.charAt(i) == '1') {
count++;
}
}
return count;
}
}
你可以想象一个n位二进制的数,找出所有只有k个1,其他位都是0的数,这个二进制数的第x位为1就表示取字母表中的第x个字母,为0不取,最后得到的就是这个二进制数代表的组合,将所有的二进制数都翻译成字母组合后,就是你要取得的所有字母组合了。
如果实在不会的话,待会再给你写个代码
public class Combination {
public static void main(String[] args) {
String[] valueSets = { "a", "b", "c", "d", "e" };
int n = 3;
List<String> list = combination(valueSets, n);
System.out.println(list);
for(String string: list){
System.out.println(string);
}
System.out.println("一共 "+list.size()+" 个。");
}
public static List<String> combination(String[] valueSets, int n) {
System.out.println(">>>>>combination");
List<String> binaryList = searchBinaryList(valueSets.length, n);
List<String> combinationList = toCombinationList(binaryList, valueSets);
return combinationList;
}
public static List<String> toCombinationList(List<String> binaryList,
String[] valueSets) {
List<String> combinationList = new ArrayList<String>();
for (String binary : binaryList) {
String combination = changeBinaryToCombination(binary, valueSets);
if (combination != null && combination.trim() != "") {
combinationList.add(combination);
}
}
return combinationList;
}
public static String changeBinaryToCombination(String binary,
String[] valueSets) {
String combination = "";
if (binary == null || binary.trim() == "") {
return null;
}
for (int i = 0; i < binary.length(); i++) {
if (binary.charAt(i) == '1') {
combination += valueSets[i];
}
}
return combination;
}
public static List<String> searchBinaryList(int length, int n) {
System.out.println(">>>>>searchBinaryList");
List<String> binaryList = new ArrayList<String>();
for (int i = 0; i < (int) Math.pow(2, length); i++) {
String binary = Integer.toBinaryString(i);
int count = oneCountsContainsInBinary(binary);
if (count == n) {
binaryList.add(toSpecifiedBitsBinary(binary, length));
}
}
return binaryList;
}
public static String toSpecifiedBitsBinary(String binary, int length) {
String specifiedBitsBinary = "";
for (int i = 0; i < length - binary.length(); i++) {
specifiedBitsBinary += 0;
}
specifiedBitsBinary += binary;
return specifiedBitsBinary;
}
public static int oneCountsContainsInBinary(String binary) {
int count = 0;
if (binary == null || binary.trim() == "") {
return count;
}
for (int i = 0; i < binary.length(); i++) {
if (binary.charAt(i) == '1') {
count++;
}
}
return count;
}
}
追问
不一样的,您的程序是从valueSets选3个所有排列组合。我是要将valueSets里分成M组,也就是valueSets所有的数都需要分配出去
展开全部
把Java中的集合,数组,字符串整合成一个统一的api。很类似JQuery中的著名的$函数。
看例子吧:
// 产生 0, 1, 2, 3, 4序列
List list = $(5).toList();
// 指定开始结束的序列 10, 11, 12, 13, 14
List list2 = $(10, 15).toList();
// 产生随机数序列
List randomIntegers = $(new Random(), 100).samples(50).sort().toList();
// jion方法,我太喜欢了。"4->3->2->1->0"
$(5).reverse().join("->");
// join() any iterable to a String
$(array).join();
$(list).join();
$(set).join();
// join() works also for 'range' type
$(5, 10).join(); // "56789"
$('a', 'd').join("-"); // "a-b-c-d"
// and even on strings, StringBuilders, etc
$("aaa").join(" "); // "a a a"
// repeat Strings, but it works also for any Iterable/Collection and
Array
String repeat = $("abc").repeat(3).join(" "); // "abc abc abc"
// convert Long[] to long[], while sorting it
Long[] aLongArray = { 42L, 84L };
long[] longs = $(aLongArray).sort().toLongArray();
// fluent interface: convert a primitive int array to ArrayList, in the
reversed order
int[] array = { 4, 2, -5 };
List list = $(array).sort().reverse().toList();
// you can even unbox a short[] array to long[] (since it doesn't cause a
loss of precision)
short[] shorts = { 42, 42, 42 };
long[] $(shorts).toLongArray();
// syntactic sugar
$("a string").size(); // "a string".length()
$(aCollection).size(); // aCollection.size()
$(anArray).size(); // anArray.length
$(array).toString(); // calls Arrays.toString()
// for loop以前这么写
for (int i = 0; i < 10; i++) {
}
// 现在只需要
for (int i : $(10)) {
}
//查找 排序 取出最大最小值
$(any).indexOf(T)
$(any).sort(Comparator)
$(any).max(Comparator)
$(any).min(Comparator)
看例子吧:
// 产生 0, 1, 2, 3, 4序列
List list = $(5).toList();
// 指定开始结束的序列 10, 11, 12, 13, 14
List list2 = $(10, 15).toList();
// 产生随机数序列
List randomIntegers = $(new Random(), 100).samples(50).sort().toList();
// jion方法,我太喜欢了。"4->3->2->1->0"
$(5).reverse().join("->");
// join() any iterable to a String
$(array).join();
$(list).join();
$(set).join();
// join() works also for 'range' type
$(5, 10).join(); // "56789"
$('a', 'd').join("-"); // "a-b-c-d"
// and even on strings, StringBuilders, etc
$("aaa").join(" "); // "a a a"
// repeat Strings, but it works also for any Iterable/Collection and
Array
String repeat = $("abc").repeat(3).join(" "); // "abc abc abc"
// convert Long[] to long[], while sorting it
Long[] aLongArray = { 42L, 84L };
long[] longs = $(aLongArray).sort().toLongArray();
// fluent interface: convert a primitive int array to ArrayList, in the
reversed order
int[] array = { 4, 2, -5 };
List list = $(array).sort().reverse().toList();
// you can even unbox a short[] array to long[] (since it doesn't cause a
loss of precision)
short[] shorts = { 42, 42, 42 };
long[] $(shorts).toLongArray();
// syntactic sugar
$("a string").size(); // "a string".length()
$(aCollection).size(); // aCollection.size()
$(anArray).size(); // anArray.length
$(array).toString(); // calls Arrays.toString()
// for loop以前这么写
for (int i = 0; i < 10; i++) {
}
// 现在只需要
for (int i : $(10)) {
}
//查找 排序 取出最大最小值
$(any).indexOf(T)
$(any).sort(Comparator)
$(any).max(Comparator)
$(any).min(Comparator)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
求所有组合什么意思?
追问
如M=3,N=2时,假设数组a={a,b,c},想要结果为:{a}{b,c} 、{a,b}{c}、{a,c}{b}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
额 可以把问题说清楚点么 不太懂你的意思
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询