Sub MYCMB()
Const t = 5, Z = 8 '从8个数字中取顷配5个进行组合
Dim CNO, q(), CM(), CM2()
st = Timer
'为保证速度,用数组存储结果
ReDim q(1 To t)
ReDim CM(1 To WorksheetFunction.combin(Z, t))
nq 1, 1, t, Z, CNO, q(), CM()
'转二维数丛迅组,以便EXCEL存放
ReDim CM2(1 To CNO, 1 To t)
For i = 1 To CNO
For j = 1 To t
CM2(i, j) = CM(i)(j)
Next j
Next i
'输出结果到表格
Cells(1, t + 2) = "组合数"
Cells(1, t + 3) = CNO
If CNO >65536 Then CNO = 65536
Range(Cells(1, 1), Cells(CNO, t)) = CM2
Cells(2, t + 2) = "运行时间(秒)"
Cells(2, t + 3) = Timer - st
End Sub
'递归函数
Sub nq(n, s, x, E, CNO, q(), CM())
For i = s To E - x + n
q(n) = i
If n = x Then '当前组合的数字已经选完
CNO = CNO + 1
CM(CNO) = q
Else
nq n + 1, i + 1, x, E, CNO, q(), CM()
End If
Next i
End Sub
希望能解决您的问题。
15位的排列太大了,你恐怕没有考虑到岁慧,我这个例子只给了5位的一个排列,仅供参考import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream
public class Bd9Test {
BufferedOutputStream buffout
int combine(char a[], int n, int m) {
m = m >n ? n : m
int[] order = new int[m + 1]
for (int i = 0i <= mi++)
order[i] = i - 1// 注意这里order[0]=-1用来作为循环判断标识
int count = 0
int k = m
boolean flag = true// 标志乎敬答找到一个有效组合
while (order[0] == -1) {
if (flag) // 输出符合要求的组合
{
StringBuffer sb = new StringBuffer()
for (int i = 1i <= mi++)
sb.append(a[order[i]])
/***** 如果你不需要排列只要组合,删除这段就可以了然后稿厅打印sb.toString()****/
PermutationGenerator x = new PermutationGenerator(sb.toString().length())
StringBuffer permutation
int[] indices
while (x.hasMore()) {
permutation = new StringBuffer()
indices = x.getNext()
for (int i = 0i <indices.lengthi++) {
permutation.append(sb.toString().charAt(indices[i]))
}
System.out.println(permutation.toString())
// try {
// buffout.write((permutation.toString()+"\r\n").getBytes())
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace()
// }
}
/***** 如果你不需要排列只要组合,删除这段就可以了然后打印sb.toString()****/
count++
flag = false
}
order[k]++// 在当前位置选择新的数字
if (order[k] == n) // 当前位置已无数字可选,回溯
{
order[k--] = 0
continue
}
if (k <m) // 更新当前位置的下一位置的数字
{
order[++k] = order[k - 1]
continue
}
if (k == m)
flag = true
}
order = null
return count
}
public static void main(String[] args) throws Exception {
char[] car= {'A','B','C','D','E'}
FileOutputStream out = new FileOutputStream(new File("C:/add0.txt"))
Bd9Test bt = new Bd9Test()
bt.buffout =new BufferedOutputStream(out)
for (int i = 2i <car.length+1i++) {
bt.combine(car, car.length, i)
}
bt.buffout.flush()
bt.buffout.close()
}
}
import java.math.BigInteger
public class PermutationGenerator {
private int[] a
private BigInteger numLeft
private BigInteger total
public PermutationGenerator(int n) {
if (n <1) {
throw new IllegalArgumentException("Min 1")
}
a = new int[n]
total = getFactorial(n)
reset()
}
public void reset() {
for (int i = 0i <a.lengthi++) {
a[i] = i
}
numLeft = new BigInteger(total.toString())
}
public BigInteger getNumLeft() {
return numLeft
}
public BigInteger getTotal() {
return total
}
public boolean hasMore() {
return numLeft.compareTo(BigInteger.ZERO) == 1
}
private static BigInteger getFactorial(int n) {
BigInteger fact = BigInteger.ONE
for (int i = ni >1i--) {
fact = fact.multiply(new BigInteger(Integer.toString(i)))
}
return fact
}
public int[] getNext() {
if (numLeft.equals(total)) {
numLeft = numLeft.subtract(BigInteger.ONE)
return a
}
int temp
// Find largest index j with a[j] <a[j+1]
int j = a.length - 2
while (a[j] >a[j + 1]) {
j--
}
// Find index k such that a[k] is smallest integer
// greater than a[j] to the right of a[j]
int k = a.length - 1
while (a[j] >a[k]) {
k--
}
// Interchange a[j] and a[k]
temp = a[k]
a[k] = a[j]
a[j] = temp
// Put tail end of permutation after jth position in increasing order
int r = a.length - 1
int s = j + 1
while (r >s) {
temp = a[s]
a[s] = a[r]
a[r] = temp
r--
s++
}
numLeft = numLeft.subtract(BigInteger.ONE)
return a
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)