这里有一个问题,取所有1-n组合的时候程序正常,但如果取单一的c(n,m)组合时会有问题。
如取c(20,2)就会内存溢出,但取一个c(20,10)又正常,本人还没明白什么问题,请高手指教。
package stats.hotdeck;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 取所有组合的内部类
*
*/
public class Combination {
private int[] index;// 用于存储需要组合的数组的下标的成员变量。
private int length;// 表示待组合数组的长度。
private int n;// 组合序列的元素个数
private long numLeft;// 用于存储剩余组合序列个数的成员变量。可以用double型,如果数量级足够大的话。
// private long total;//用于存储组合序列总数的成员变量。
private int[] intArray;// 存放待组合数组下标的数组。
public Combination(int length) {
this(length, 0);
}
public Combination(int length, int n) {
this.length = length;
this.n = n;
reset();
initIntArray();
}
public void reset() {
if (n > length) {
System.out.println("需要组合的个数超出数组元素个数!");
System.exit(1);
}
// 初始化numLeft,开始时numLeft应该为2^n.
// 如果是double,则转型为double,否则会取漏组合
numLeft = (long) Math.pow((double) 2, (double) length);
// 初始化数组index。
index = new int[length];
Arrays.fill(index, 0);
}
private int sum() {
int s = 0;
for (int i = 0; i < index.length; i++) {
s += index[i];
}
return s;
}
public boolean hasMore() {
return numLeft > 0;
}
public int[] getNext() {
index[0] += 1;
for (int i = 0; i < index.length; i++) {
if (index[i] == 2) {
index[i] = 0;
if (i != index.length - 1)
index[i + 1] += 1;
}
}
numLeft--;
if (this.n != 0) {
if (sum() == this.n)
return index;
else if (hasMore())
return getNext();
}
return index;
}
protected int[] getIntArray() {
return intArray;
}
/**
* 按默认的顺序从0开始赋值,初始化intArray
*/
protected void initIntArray() {
this.intArray = new int[this.length];
for (int i = 0; i < this.length; i++) {
intArray[i] = i;
}
}
/**
* 自定义初始化intArray
*/
protected void initIntArray(int[]arr) {
this.intArray=arr;
}
/**
* 得到所有以数组下标为组合的所有组合的集合
* @return
*/
protected List getAllCombination(){
List combList=new ArrayList();
List oneComb=null;
int a=0;
while (hasMore()) {
oneComb=new ArrayList();
int[] index = getNext();
for (int j = 0; j < intArray.length; j++) {
if (index[j] != 0) {
a = intArray[index[j] * j];
oneComb.add(new Integer(a));
}
}
if(!oneComb.isEmpty()){
combList.add(oneComb);
}
}
return combList;
}
/**
* 得到所有以对象元素组合的所有组合的集合
* @param objectList 进行组合的对象集合
* @return
*/
protected List getAllCombinationForObject(List objectList){
List combList=new ArrayList();
List oneComb=null;
int a=0;
while (hasMore()) {
oneComb=new ArrayList();
int[] index = getNext();
for (int j = 0; j < intArray.length; j++) {
if (index[j] != 0) {
a = intArray[index[j] * j];
oneComb.add(objectList.get(a));
}
}
if(!oneComb.isEmpty()){
combList.add(oneComb);
}
}
return combList;
}
public static void main(String[] args){
Combination comb=new Combination(20,10);
List list=comb.getAllCombination();
System.out.print(list.size());
}
}
如取c(20,2)就会内存溢出,但取一个c(20,10)又正常,本人还没明白什么问题,请高手指教。
package stats.hotdeck;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 取所有组合的内部类
*
*/
public class Combination {
private int[] index;// 用于存储需要组合的数组的下标的成员变量。
private int length;// 表示待组合数组的长度。
private int n;// 组合序列的元素个数
private long numLeft;// 用于存储剩余组合序列个数的成员变量。可以用double型,如果数量级足够大的话。
// private long total;//用于存储组合序列总数的成员变量。
private int[] intArray;// 存放待组合数组下标的数组。
public Combination(int length) {
this(length, 0);
}
public Combination(int length, int n) {
this.length = length;
this.n = n;
reset();
initIntArray();
}
public void reset() {
if (n > length) {
System.out.println("需要组合的个数超出数组元素个数!");
System.exit(1);
}
// 初始化numLeft,开始时numLeft应该为2^n.
// 如果是double,则转型为double,否则会取漏组合
numLeft = (long) Math.pow((double) 2, (double) length);
// 初始化数组index。
index = new int[length];
Arrays.fill(index, 0);
}
private int sum() {
int s = 0;
for (int i = 0; i < index.length; i++) {
s += index[i];
}
return s;
}
public boolean hasMore() {
return numLeft > 0;
}
public int[] getNext() {
index[0] += 1;
for (int i = 0; i < index.length; i++) {
if (index[i] == 2) {
index[i] = 0;
if (i != index.length - 1)
index[i + 1] += 1;
}
}
numLeft--;
if (this.n != 0) {
if (sum() == this.n)
return index;
else if (hasMore())
return getNext();
}
return index;
}
protected int[] getIntArray() {
return intArray;
}
/**
* 按默认的顺序从0开始赋值,初始化intArray
*/
protected void initIntArray() {
this.intArray = new int[this.length];
for (int i = 0; i < this.length; i++) {
intArray[i] = i;
}
}
/**
* 自定义初始化intArray
*/
protected void initIntArray(int[]arr) {
this.intArray=arr;
}
/**
* 得到所有以数组下标为组合的所有组合的集合
* @return
*/
protected List getAllCombination(){
List combList=new ArrayList();
List oneComb=null;
int a=0;
while (hasMore()) {
oneComb=new ArrayList();
int[] index = getNext();
for (int j = 0; j < intArray.length; j++) {
if (index[j] != 0) {
a = intArray[index[j] * j];
oneComb.add(new Integer(a));
}
}
if(!oneComb.isEmpty()){
combList.add(oneComb);
}
}
return combList;
}
/**
* 得到所有以对象元素组合的所有组合的集合
* @param objectList 进行组合的对象集合
* @return
*/
protected List getAllCombinationForObject(List objectList){
List combList=new ArrayList();
List oneComb=null;
int a=0;
while (hasMore()) {
oneComb=new ArrayList();
int[] index = getNext();
for (int j = 0; j < intArray.length; j++) {
if (index[j] != 0) {
a = intArray[index[j] * j];
oneComb.add(objectList.get(a));
}
}
if(!oneComb.isEmpty()){
combList.add(oneComb);
}
}
return combList;
}
public static void main(String[] args){
Combination comb=new Combination(20,10);
List list=comb.getAllCombination();
System.out.print(list.size());
}
}