Java 应用递归求子集和
我试图制作一个代码,将子集中的所有整数相加,看看它们的总和是否为零。这是我到目前为止得到的Java 应用递归求子集和,java,subset,Java,Subset,我试图制作一个代码,将子集中的所有整数相加,看看它们的总和是否为零。这是我到目前为止得到的 /** * Solve the subset sum problem for a set of integer values. * * @param t * a set of integer values * @return <code>true</code> if there exists a non-empty subset
/**
* Solve the subset sum problem for a set of integer values.
*
* @param t
* a set of integer values
* @return <code>true</code> if there exists a non-empty subset of
* <code>t</code> whose elements sum to zero.
*/
public static boolean subsetSum(Set<Integer> t) {
return subsetSum(new ArrayList<Integer>(t), null);
}
/**
* Computes the sum of two Integers where one or both references are null. If
* both references are null then the result is null; otherwise the result is
* an integer equal to <code>a + b</code> where null references are treated as
* being equal to zero.
*
* @param a
* @param b
* @return the sum of <code>a</code> and <code>b</code>
*/
private static Integer add(Integer a, Integer b) {
if (a == null && b == null) {
return null;
} else if (a == null) {
return b;
} else if (b == null) {
return a;
}
return a + b;
}
/**
* Recursive solution for the subset sum problem.
*
* <p>
* <code>currentSum</code> holds the value of the subset under consideration;
* it should be <code>null</code> if the current subset is empty.
*
* @param t
* a list containing unique integer values (a set of integers)
* @param currentSum
* the current subset sum so far
* @return <code>true</code> if there exists a subset of <code>t</code> such
* that the sum of the elements in the subset and
* <code>currentSum</code> equals zero.
*/
******** THIS IS THE PART I HAD TO EDIT *************
private static boolean subsetSum(List<Integer> t, Integer currentSum) {
currentSum = 0;
for (int i = 0; i < t.size(); i++) {
currentSum = currentSum + (Integer)(t.get(i));
}
if (Lab9W.add(currentSum, t.get(0)) == 0) {
return true;
}
else if (Lab9W.add(currentSum, t.get(1)) == 0) {
return true;
} else if (Lab9W.add(-t.get(0),t.get(0)) == 0) {
return true;
}
else {
return false;
}
}
}
下面是我收到的关于编写此代码的提示:
首先考虑集合的第一个元素是否有
第一组和其他组的零?有零的子集和吗
没有第一个和其他人?如果2或3中的任何一个为真
然后返回true,否则返回false
任何帮助,请我已经尝试了一整天,我不能让它为我的生活工作,以及在递归我不知道如何调用自己的方法
所以我的问题是如何用递归编写这个方法?整个方法应该是将子集之和相加,看看它们是否等于零。好的,对于递归,您可以这样做
private static boolean subsetSum(List<Integer> t, Integer sizeOfArray,Integer currentSum){
if(sizeOfArray == -1)
{
if (Lab9W.add(currentSum, t.get(0)) == 0)
{
return true;
}
else if (Lab9W.add(currentSum, t.get(1)) == 0)
{
return true;
}
else if (Lab9W.add(-t.get(0),t.get(0)) == 0)
{
return true;
}
else
{
return false;
}
return subsetSum(t,sizeOfArray - 1,currentSum + t.get(sizeOfArray));
}
//
好的,你也可以这样做
// return sum of array
public Integer subsetSum(List<Integer> t, Integer sizeOfArray)
{
if(sizeOfArray == 0)
return t.get(0);
return subsetSum(t, sizeOfArray - 1) + t.get(sizeOfArray);
}
//返回数组的和
公共整数子类(列表t,整数大小)
{
如果(sizeOfArray==0)
返回t.get(0);
返回子类(t,sizeOfArray-1)+t.get(sizeOfArray);
}
//现在你可以做这件事了
public boolean check(List<Integer> t)
{
Integer currentSum = subsetSum(t,t.size()-1);
if (Lab9W.add(currentSum, t.get(0)) == 0)
{
return true;
}
else if (Lab9W.add(currentSum, t.get(1)) == 0)
{
return true;
}
else if (Lab9W.add(-t.get(0),t.get(0)) == 0)
{
return true;
}
else
{
return false;
}
}
公共布尔检查(列表t)
{
整数currentSum=子项(t,t.size()-1);
if(Lab9W.add(currentSum,t.get(0))==0)
{
返回true;
}
else if(Lab9W.add(currentSum,t.get(1))==0)
{
返回true;
}
else if(Lab9W.add(-t.get(0),t.get(0))==0)
{
返回true;
}
其他的
{
返回false;
}
}
//返回数组的和
公共整数子类(列表t,整数大小)
{
如果(sizeOfArray==0)
返回t.get(0);
返回子类(t,sizeOfArray-1)+t.get(sizeOfArray);
}
//现在你可以做这件事了
公共布尔检查(列表t)
{
整数currentSum=子项(t,t.size()-1);
if(Lab9W.add(currentSum,t.get(0))==0)//处理这些if语句
{
返回true;
}
else if(Lab9W.add(currentSum,t.get(1))==0)
{
返回true;
}
else if(Lab9W.add(-t.get(0),t.get(0))==0)
{
返回true;
}
其他的
{
返回false;
}
}
与这个问题类似吗?你如何解决这个问题
给定一套完整重量的石头,可以按重量将其分为两组
完全相等。
编写一个递归函数
公共静态布尔canSplit(int[]石头,int左,int右)
其中,给定一组权重,平均函数可分为两组)左
正确-(这样整个团队都值得。如果需要,使用函数子数组。
培训:看第一块石头,递归决定哪个组与之关联。您试图检查的是数组中的第一个元素+所有元素的总和等于0?是的,我是@JavaNewb,因为这就是建议的原因否?您的整个逻辑根本没有意义。我怀疑您是否真正理解问题所在。假设集合中有5个整数:a、b、c、d、e。如果这些整数中的任何一个加起来等于0,则应返回true。例如,如果a+b+d==0,则返回true。您的逻辑与此无关。您只需将整个列表相加,如果第一个/第二个元素的总和+值(再次!)==0,或者第一个元素不是null,然后返回true。这个逻辑的意义是什么?我不确定这就是为什么我感到困惑我不明白我非常困惑:(@adrianshumok所以你只是在尝试使用递归检查元素之和是否等于0?是的,但是当我应用你修改过的代码时,它给了我一个错误:必须返回布尔类型:/@JavaNewb也是私有静态布尔子集(列表t,整数currentSum)必须是一样的我不认为我可以编辑这个对吧我没有测试过,但我只是编辑了一下,我添加了一个return你不能添加一个新参数吗?不,我不这么认为:(@javanewb如果我可以返回一个整数,我可以这样做,是吗?那么我返回当前的总和,然后检查if语句,这样可以吗?
public boolean check(List<Integer> t)
{
Integer currentSum = subsetSum(t,t.size()-1);
if (Lab9W.add(currentSum, t.get(0)) == 0)
{
return true;
}
else if (Lab9W.add(currentSum, t.get(1)) == 0)
{
return true;
}
else if (Lab9W.add(-t.get(0),t.get(0)) == 0)
{
return true;
}
else
{
return false;
}
}
// return sum of array
public Integer subsetSum(List<Integer> t, Integer sizeOfArray)
{
if(sizeOfArray == 0)
return t.get(0);
return subsetSum(t, sizeOfArray - 1) + t.get(sizeOfArray);
}
//now you can do this somewere
public boolean check(List<Integer> t)
{
Integer currentSum = subsetSum(t,t.size()-1);
if (Lab9W.add(currentSum, t.get(0)) == 0) // do something with these if statements
{
return true;
}
else if (Lab9W.add(currentSum, t.get(1)) == 0)
{
return true;
}
else if (Lab9W.add(-t.get(0),t.get(0)) == 0)
{
return true;
}
else
{
return false;
}
}