简体   繁体   English

Boolean 包含 5 的倍数的递归

[英]Boolean Recursion with multiples of 5 included

This is the problem I'm working on: "Given an array of ints, is it possible to choose a group of some of the ints, such that the group sums to the given target with these additional constraints: all multiples of 5 in the array must be included in the group. If the value immediately following a multiple of 5 is 1, it must not be chosen. (No loops needed.)"这是我正在处理的问题:“给定一个整数数组,是否可以选择一组整数,使得该组在这些附加约束下与给定目标相加:所有 5 的倍数数组必须包含在组中。如果紧跟在 5 的倍数后面的值为 1,则不能选择它。(不需要循环。)"

I tried the following:我尝试了以下方法:

public boolean groupSum5(int start, int[] nums, int target) {
  if (start == nums.length) return (target == 0);
  if (groupSum5(start + 1, nums, target - nums[start]) && nums[start] % 5 == 0)
    return true;
  if (groupSum5(start + 1, nums, target)) return true;
  return false;
}

But it only gets the multiples of 5, and I tried this:但它只能得到 5 的倍数,我试过这个:

public boolean groupSum5(int start, int[] nums, int target) {
  if (start == nums.length) return (target == 0);
  if (groupSum5(start + 1, nums, target - nums[start]) && nums[start] % 5 == 0)
    return true;
  if (groupSum(start + 1, nums, target - nums[start])) return true;
  if (groupSum5(start + 1, nums, target)) return true;
  return false;
}

but it does not work, since sometimes the multiples of 5 are not included.但它不起作用,因为有时不包括 5 的倍数。

I know my code does not fullfil the second constraint, yet.我知道我的代码还没有满足第二个约束。

Any ideas?有任何想法吗?

Edit:编辑: 在此处输入图像描述

if (groupSum5(start + 1, nums, target - nums[start]))

For each number in your input, you need to choose whether to include it, or not include it.对于输入中的每个数字,您需要选择是否包含它。 This if is the 'include it' option: That's why you reduce the target by the value.if是“包含它”选项:这就是您将目标减少值的原因。

&& nums[start] % 5 == 0

... so this means: It is impossible to include any given value, unless it is a multiple of 5. Which is not what the problem description wants you to do! ...所以这意味着:不可能包含任何给定的值,除非它是 5 的倍数。这不是问题描述要你做的!

What the problem description wants you to do is: If the number you're on is a multiple of 5, then it MUST be included.问题描述要你做的是:如果你所在的数字是 5 的倍数,那么它必须包括在内。 You cannot opt not to include it.您不能选择不包含它。

Thus, you are sticking the constraint on the wrong if, and the wrong way around.因此,您将约束限制在错误的 if 和错误的方式上。 What you actually want is for the second if, which covers the 'lets not pick this number' to be modified:您真正想要的是第二个 if,它涵盖了要修改的“让不要选择这个数字”:

if (!num[start] % 5 == 0 && groupSum5(start, nums, target)) return true;

In other words: If the number we are on is NOT a multiple of 5, then try not including this number and see if that works out.换句话说:如果我们所在的数字不是 5 的倍数,那么试着不包括这个数字,看看是否可行。 (Because trying to not include it when it IS a multiple of 5, is invalid). (因为当它是 5 的倍数时试图不包括它是无效的)。

If the value immediately following a multiple of 5 is 1, it must not be chosen.如果紧跟在 5 的倍数后面的值为 1,则不能选择它。

That is another constraint, and this one must be applied to the first if (the one that represents 'lets include this number').这是另一个约束,这个约束必须应用于第一个 if(代表“让包括这个数字”的那个)。 That if needs some additional && to filter -OUT- the case where the number you are on is a 1, AND it is not the first number in the sequence, AND the previous number in the sequence is a multiple of 5: In that case, 'include the number' is not a valid move.如果需要一些额外的&&来过滤 -OUT- 你所在的数字是 1,并且它不是序列中的一个数字,并且序列中的前一个数字是 5 的倍数:在这种情况下, '包括号码' 不是有效的移动。

Use inverted conditions (with a ! in front) and && , analogous to the above example.使用反转条件(前面有! )和&& ,类似于上面的示例。 What you want to accomplish is that the step of 'try including this number and then see if we can work it out by applying this algorithm to the remainder of the list' is simply skipped if that step isn't valid due to the additional constraints.您想要完成的是,如果由于附加约束而导致该步骤无效,则简单地跳过“尝试包含此数字,然后看看我们是否可以通过将此算法应用于列表的其余部分来解决它”的步骤.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM