回溯算法 - 子集和问题
(1)问题描述:子集和问题的一个实例为<data, num>。其中 data = {x 1 , x 2 , ......, x n } 是一个正整数的集合,targetValue 是一个正整数。子集和问题判定是否存在 data 的一个子集 data1,使得 x 1 + x 2 + ...... + x n = targetValue (x € data1) (2)算法设计:使用回溯算法子集树来解决,对于给定的集合 data = {x 1 , x 2 , ......, x n } 和正整数 targetValue,计算 data 的一个子集 data1,满足【x 1 + x 2 + ...... + x n = targetValue (x € data1)】 (3)算法代码: public class SubsetSum { /** * 目标值 */ private static Integer targetValue; /** * 当前所选元素之和 */ private static Integer sum = 0; /** * 数据个数 */ private static Integer num; /** * 未确定值 */ private static Integer indeterminacyValue = 0; /** * 数据数组 */ private static