◆◆关于算法（急）◆◆

1. 色子(6个面),N个色子,把所有组合输出来,去掉重复的(如:3个色子: 1,2,3的组合 和 3,2,1的组合 或者 2,3,1 的组合 都示为重复的 )
2. 画TABLE,所有单元格相临的颜色都不能有重复的.

------重点希望有朋友可以解答第1题. 因为我只做了第1题. 5小时.还没做出来.-------

13个回答

public class DiceArith {

``````/**
* 数组转字符
*
* @return
*/
public static String arraysToString(String[] s) {
String t = "";
for (int i = 0; i < s.length; i++)
t += s[i];
return t;
}

/**
* 方法一
*
* @param N
*/
public static void method1(int N) {
TreeSet<String> tree = new TreeSet<String>();
long start = 1, end = 6;
for (int i = 1; i < N; i++) {
start += 1 * convert(10, i);
end += 6 * convert(10, i);
}
String[] data;
for (long j = start; j <= end; j++) {
if (expValid(j + "")) {
data = String.valueOf(j).split("");
Arrays.sort(data);
}
}

for (Iterator<String> t = tree.iterator(); t.hasNext();) {
String d = t.next();
System.out.println(d);
}
}

public static void main(String[] args) {
method1(3);
}

/**
* 计算平方根
*
* @param data
* @param n
* @return
*/
public static long convert(int data, int n) {
long result = 1l;
for (int i = 1; i <= n; i++)
result *= data;
return result;
}

/**
* 正则验证色子数在【1-6】范围内
*
* @param s
* @return
*/
public static boolean expValid(String s) {
Pattern p4 = Pattern.compile("^[1-6]+\$");
Matcher m4 = p4.matcher(s + "");
if (m4.find())
return true;
else
return false;
}
``````

}

N 个色子, 每个取值 1~6, 要求没有重复的组合, 则组合为这样的
11111111111...1 (N 个 1),
12345612345...6 (N 个数),
...

[quote]能不能给出具体的代码。关于理论的想法我也想过很多。

int start = 1, end = 6;
for (int i = 1; i < N; i++) {
start += 1* 10 * i;
end += 6*10*i;
}
for (int i = start; i <=end; i++)
System.out.println(i);

public class DiceArith {

``````public static long convert(int data, int n) {
long result = 1l;
for (int i = 1; i <= n; i++)
result *= data;
return result;
}

public static void main(String[] args) {
long start = 1, end = 6;
long N = 4;
for (int i = 1; i < N; i++) {
start += 1 * convert(10, i);
end += 6 * convert(10, i);
}
for (long j = start; j <= end; j++)
System.out.println(j);
}
``````

}

[quote]for (int i = start; i <=end; i++)
System.out.println(i);[/quote]

17..27..37..47..57.了吗，请问色子有7吗？

[code="java"]for (int i = start; i <=end; i++) {
Pattern p4=Pattern.compile("[1-6]+");
Matcher m4=p4.matcher(i+"");
if(m4)
System.out.println(i);
}[/code]

for (int i = start; i <=end; i++) {
if (i.toString().indexOf(55) != -1 || i.toString().indexOf(56) != -1 || i.toString().indexOf(57) != -1 || i.toString().indexOf(48) != -1)
continue;

System.out.println(i);
}

[quote]但我觉得你们这样做循环，效率都不高，我觉得让一个组合作成六进制的数，只要加到6就进1，那样会省去很多多余的组合。估计效率会高点[/quote]

public class DiceArith {

``````/**
* 数组转字符
*
* @return
*/
public static String arraysToString(String[] s) {
String t = "";
for (int i = 0; i < s.length; i++)
t += s[i];
return t;
}

/**
* 方法一
*
* @param N
*/
public static void method1(int N) {
String data = "", temp = "";
TreeSet<String> tree = new TreeSet<String>();
for (int i = 1; i <= N; i++) {
for (int k = 1; k <= 6; k++) {
data = i + "" + k;
String[] arr = data.split("");
Arrays.sort(arr);
temp = arraysToString(arr);
}
}

for (Iterator<String> t = tree.iterator(); t.hasNext();) {
String d = t.next();
System.out.println(d);
}
}

public static void main(String[] args) {
method1(4);
}
``````

}