有限集 S 的简单 k 组合是 S 的 k 个不同元素的子集。指定子集不会以特定顺序排列它们。
您可以使用 CombinatoricsLib。CombinatoricsLib 是一个用于生成组合对象的 java 库。https://code.google.com/p/combinatoricslib/
使用这个:
public static void main(String[] args) {
// Create the initial vector
ICombinatoricsVector<Integer> initialVector = Factory.createVector(
new Integer[] {1, 5, 4, 3, 6, 9, 7, 4, 3, 8, 2, 1, 6, 3, 7} );
int subsetMaxSize = 5;
int upperLimit = 10;
int lowerLimit = 8;
for(int i = 1; i <= subsetMaxSize; i++)
{
Generator<Integer> gen = Factory.createSimpleCombinationGenerator(initialVector, i);
for (ICombinatoricsVector<Integer> combination : gen)
{
int sum = vectorSum(combination);
if(validateSum(sum, lowerLimit, upperLimit))
printVector(combination);
}
}
}
public static boolean validateSum(Integer value, Integer lowerLimit, Integer upperLimit)
{
if(value <= upperLimit && value > lowerLimit)
return true;
return false;
}
public static Integer vectorSum(ICombinatoricsVector<Integer> vect)
{
Integer sum = 0;
for(int i = 0; i < vect.getSize(); i++)
sum += vect.getValue(i);
return sum;
}
public static void printVector(ICombinatoricsVector<Integer> vect)
{
String output = "";
for(int i = 0; i < vect.getSize(); i++)
output += vect.getValue(i) + ", ";
System.out.println(output);
}
将返回输出
9,
1, 9,
1, 8,
5, 4,
5, 4,
4, 6,
4, 6,
3, 6,
3, 7,
3, 6,
3, 7,
6, 4,
6, 3,
6, 3,
9, 1,
7, 3,
7, 2,
7, 3,
4, 6,
3, 6,
3, 7,
8, 2,
8, 1,
2, 7,
6, 3,
3, 7,
1, 5, 4,
1, 5, 3,
1, 5, 4,
1, 5, 3,
1, 5, 3,
1, 4, 4,
1, 3, 6,
1, 3, 6,
1, 6, 3,
1, 6, 2,
1, 6, 3,
1, 7, 2,
1, 7, 1,
1, 3, 6,
1, 8, 1,
1, 2, 6,
1, 2, 7,
1, 1, 7,
1, 6, 3,
5, 4, 1,
5, 3, 2,
5, 3, 1,
5, 4, 1,
5, 3, 2,
5, 3, 1,
5, 2, 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