/*方便运行省略包名*/
public class ChildCollection {
public static void main(String[] args) {
int[] parentCollection = new int[]{5, 7, 1, 4, 3, 2};
int initSum = 13;
int tempSum, tempValue;
for(int i = 0; i < Math.pow(2, parentCollection.length); i++) {
tempValue = i;
tempSum = 0;
for(int j = 0; j < parentCollection.length; j++) {
if(tempValue%2 == 1)
tempSum += parentCollection[j];
tempValue /= 2;
}
if(tempSum == initSum) {
tempValue = i;
for(int j = 0; j < parentCollection.length; j++) {
if(tempValue%2 == 1)
System.out.print(" " + parentCollection[j]);
tempValue /= 2;
}
System.out.println("\n");
}
}
}
}
给出一个整数,判断集合中是否存在子集之和等于该整数?
最新推荐文章于 2020-10-28 17:18:25 发布