整数を指定し、指定されたセットにサブセットの和が存在するかどうかを判断します.


public class ChildCollection {
     

    public static Object isChildCollection(final int[] set, final int num) {
     
        //1.    tempSum tempValue,        
        int tempSum,tempValue;
        //2.    result,     
        boolean result = false;
        //3.  Math.pow(  ,   )  ,  2      ,(           )
        for (int i = 0; i < Math.pow(2, set.length); i++) {
     
            //4.    sum,            
            int sum = 0;
            tempSum = i;
            for (int j = 0; j < set.length; j++) {
     
                //5.    ,  tempSum%2    1(    ),  1,      
                if (tempSum % 2 == 1) {
     
                    sum += set[j];
                }
                tempSum /= 2;
            }
            //6.  sum      num  ,    ,  true
            if (sum == num) {
     
                tempValue = i;
                System.out.println("     :" );
                //7.            
                for(int j = 0;j < set.length;j++){
     
                    if(tempValue % 2 == 1){
     
                        System.out.print(set[j]+ "," );
                    }
                    tempValue /= 2;
                }
                System.out.println("");
                result = true;
            }
        }
        return result;
    }

        public static void main (String[]args){
     
            int set[] = {
     4, 14, 5, 9, 1, 17};
            int sum = 10;
            System.out.println(isChildCollection(set, sum));
        }
    }