java传入一个数组{1,2,3},返回123,132,213,231,312,321,求指导.
来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/10/02 06:28:01
java传入一个数组{1,2,3},返回123,132,213,231,312,321,求指导.
假如不限制数组个数
假如不限制数组个数
这个,你看一下,LEN可以自己改.
-------------------------------------------------------------------------
public class demo {
public static void main(String[] args) {
\x05int len = 3;
\x05String[] array = { "1","2","3" };
\x05execute(array,len,"");
}
public static void execute(String[] array,int len,String str) {
\x05for (int i = 0; i < array.length; i++) {
\x05 str += array[i];
\x05 if (str.length() == len) {
\x05\x05System.out.println(str);
\x05 } else {
\x05\x05execute(array,len,str);
\x05 }
\x05 str = str.substring(0,str.length() - 1);
\x05}
}
}
再问: 不限制数组的个数
再答: 昨天晚上改过了,不知道怎么又变回来了 你看一下这个。 ---------------------------------------------------------------------------------------- import java.util.ArrayList; import java.util.List; public class demo { public static void main(String[] args) { String[] array = { "1", "2", "3", "4" }; List list = new ArrayList(); execute(array, list); } public static void execute(String[] array, List list) { for (int i = 0; i < array.length; i++) { if (list.contains(array[i])) { continue; } list.add(array[i]); if (list.size() == array.length) { String str = ""; for (int n = 0; n < list.size(); n++) { str += list.get(n); } System.out.println(str); } else { execute(array, list); } list.remove(list.size() - 1); } } }
再问: 能说下大概思路吗
再答: 循环array list记录放入了那些值, 如果放过了就忽略那个数, 没放过就在list中加入,进入递归过程。 当LIST的长度与array 相等,输出LIST中的。
再问: 不明白add和remove
再答: add是添加元素 remove是移除 用List好判断那些元素添加了, 因为你是不重复的排列.
-------------------------------------------------------------------------
public class demo {
public static void main(String[] args) {
\x05int len = 3;
\x05String[] array = { "1","2","3" };
\x05execute(array,len,"");
}
public static void execute(String[] array,int len,String str) {
\x05for (int i = 0; i < array.length; i++) {
\x05 str += array[i];
\x05 if (str.length() == len) {
\x05\x05System.out.println(str);
\x05 } else {
\x05\x05execute(array,len,str);
\x05 }
\x05 str = str.substring(0,str.length() - 1);
\x05}
}
}
再问: 不限制数组的个数
再答: 昨天晚上改过了,不知道怎么又变回来了 你看一下这个。 ---------------------------------------------------------------------------------------- import java.util.ArrayList; import java.util.List; public class demo { public static void main(String[] args) { String[] array = { "1", "2", "3", "4" }; List list = new ArrayList(); execute(array, list); } public static void execute(String[] array, List list) { for (int i = 0; i < array.length; i++) { if (list.contains(array[i])) { continue; } list.add(array[i]); if (list.size() == array.length) { String str = ""; for (int n = 0; n < list.size(); n++) { str += list.get(n); } System.out.println(str); } else { execute(array, list); } list.remove(list.size() - 1); } } }
再问: 能说下大概思路吗
再答: 循环array list记录放入了那些值, 如果放过了就忽略那个数, 没放过就在list中加入,进入递归过程。 当LIST的长度与array 相等,输出LIST中的。
再问: 不明白add和remove
再答: add是添加元素 remove是移除 用List好判断那些元素添加了, 因为你是不重复的排列.
java传入一个数组{1,2,3},返回123,132,213,231,312,321,求指导.
将两个数组 排列组合到一个数组集合 求java 代码
编写一个函数计算1-2+3-4+…+n,其中n的值由主调函数传入,并返回计算结果(c语言)
java二维数组int[][]a={{1,2,3,},{4,5,6},{7,8,
请用Java建立一个3*3的三维数组,数组值使用随机数生成,其范围是1~50,然后计算每一个行的
编写java程序,定义一个2*3的数组a和一个3*4的数组b,在定义一个数组c,使其存放矩阵,最后输出结果
非常急!一个简单的关于求二维数组的最大值的java题目.
定义一个整型数组,int a[]={1,2,3,4,5}求数组元素之和并输出
求1 java算法 一个数组中m个数(连续的) 需要分成n组 求这n组的所有组合方式
用JAVA语言编写,从命令行传入3个整数,求3个整数的最大值、最小值.
编写一个函数计算1-2+3-4+…+n,其中n的值由主调函数传入,并返回计算结果(要完整的C语言程序)
我希望将123、132、213、231、312、321全排列输出为1,2,3,请大侠给出算法或者java代码,