您的位置:首页 > 其它

给出一个整数,判断集合中是否存在子集之和等于该整数?

2016-09-08 18:39 501 查看
/*方便运行省略包名*/
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");
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  lowTi
相关文章推荐