早教吧作业答案频道 -->其他-->
问题用c或Java写一个算法:有5个数1,2,3,4,5列出这5个数所有可能的组合?比如说一位数字有1,2,3,4,5五种可能,两位数字有,12,13,14,15,21,22等25种可能,三位数有,123,234,345等125种可能,四位,五位同理
题目详情
问题用c或Java 写一个算法 :有5个数1,2,3,4,5 列出这5个数所有可能的组合?比如说一位数字有1,2,3,4,5五种可能,两位数字有,12,13,14,15,21,22等25种可能,三位数有,123,234,345等125种可能,四位,五位同理
▼优质解答
答案和解析
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;
public class GetAssemble {
public static Set set = new TreeSet();
public static void doSet(String start, String[] sourceList, int max) {
String[] olds = start.split("_");
if (olds.length == max) {
set.add(start.replaceAll("_", "").trim());
}
else {
for (int s = 0; s < sourceList.length; s++) {
if (Arrays.asList(olds).contains(sourceList[s])) {
continue;
}
else {
doSet(start + "_" + sourceList[s], sourceList, max);
}
}
}
}
public static void doSet(String[] sourceList, int max) {
for (int start = 0; start < sourceList.length; start++) {
doSet(sourceList[start], sourceList, max);
}
}
public static void print() {
System.out.println("Total:" + set.size());
int cols = 10;
for (String s : set) {
System.out.print(s + " ");
if (cols-- == 1) {
System.out.println();
cols = 10;
}
}
set.clear();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] haha = new String[] { "1", "2", "3", "4", "5" };
System.out.println();
System.out.println(" ");
doSet(haha, 5);
print();
}
}
import java.util.Set;
import java.util.TreeSet;
public class GetAssemble {
public static Set
public static void doSet(String start, String[] sourceList, int max) {
String[] olds = start.split("_");
if (olds.length == max) {
set.add(start.replaceAll("_", "").trim());
}
else {
for (int s = 0; s < sourceList.length; s++) {
if (Arrays.asList(olds).contains(sourceList[s])) {
continue;
}
else {
doSet(start + "_" + sourceList[s], sourceList, max);
}
}
}
}
public static void doSet(String[] sourceList, int max) {
for (int start = 0; start < sourceList.length; start++) {
doSet(sourceList[start], sourceList, max);
}
}
public static void print() {
System.out.println("Total:" + set.size());
int cols = 10;
for (String s : set) {
System.out.print(s + " ");
if (cols-- == 1) {
System.out.println();
cols = 10;
}
}
set.clear();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] haha = new String[] { "1", "2", "3", "4", "5" };
System.out.println();
System.out.println(" ");
doSet(haha, 5);
print();
}
}
看了 问题用c或Java写一个算法...的网友还看了以下:
英语翻译1这是一本电子书.2这本书介绍了英语介词,并且每个介词都有例句.3英语介词被分为单个词汇介 2020-05-13 …
一列火车与一列磁悬浮列车的速度比是2:5,这列火车行驶了98千米,在相同时间内磁悬浮列车行驶多少千 2020-06-05 …
一列队伍,从左数小明排第7,从右数小明排第5,这列队伍有多少人?画图表示 2020-06-11 …
在放暑假的8月份,小明有5天是在姥姥家过的.这5天的日期除一天是合数外,其他四天的日期都是质数.这 2020-06-27 …
在一块铜和锡的合金中,铜和锡的质数比是5:3,已知铜重400千克,求这块合金中锡的质量,你能想出几 2020-07-07 …
快回答!1.一块金银合金重770克,其中金的重量是银的重量的6分之5,这块合金中金,银各多少克?2 2020-07-09 …
观察下列各数.(1)观察下列各数:1/1,-1/2,-2/1,1/3,2/2,3/1,-1/4,- 2020-07-19 …
有排列组合可以重复的公式吗?高中学的排列组合是组合Cmn=n!/m!×(n-m)!排列Amn=n!/ 2020-11-03 …
关于DNA复制这两个选项正确吗A每条互补链的合成方向是5'-3'BDNA聚合酶沿模版链滑关于DNA复 2020-12-01 …
一块合金含有锌,铜,铁,他们的重量为4;5;8,铜为60克,这块合金重多少2;一块合金含有锌,铜,铁 2020-12-02 …