【问题标题】:How do I add to a target number using an array of integers? [closed]如何使用整数数组添加到目标数字? [关闭]
【发布时间】:2016-03-20 19:41:28
【问题描述】:

如何仅使用整数数组将整数添加到某个数字?这就是我到目前为止所拥有的。我必须递归地做。

public static boolean sumsToTarget(int[] arr, int target){

    return sumsToTarget(arr, target, 0, 0, 0);

}

private static boolean sumsToTarget(int[] arr, int target, int startIndex, int endIndex, int total){
    if(endIndex >= arr.length){
        if(startIndex >= arr.length){
            return false;
        }
        return sumsToTarget(arr, target, startIndex + 1, startIndex + 1, 0);
    }
    total += arr[endIndex];
    if(total == target){
        return true;
    }else if(total >= target){
        return sumsToTarget(arr, target, startIndex, endIndex + 1, total - arr[endIndex]);
    }else{
        return sumsToTarget(arr, target, startIndex+1, startIndex+1, total) || sumsToTarget(arr, target, startIndex, endIndex + 1, total) || sumsToTarget(arr, target, startIndex, endIndex + 2, total);
    }
}

这就是我测试代码的方式。

int[] a = {1, 3, 5, 7, 9};

    System.out.println("Should always print out true.");

    try{ System.out.println("1. " + (ProjectTwo.sumsToTarget(a, 10)==true)); } catch(Exception e){ System.out.println("failed"); }
    try{ System.out.println("2. " + (ProjectTwo.sumsToTarget(a, 40)==false)); } catch(Exception e){ System.out.println("failed"); }
    try{ System.out.println("3. " + (ProjectTwo.sumsToTarget(a, 17)==true)); } catch(Exception e){ System.out.println("failed"); }

    int[] b = {-5, 100, 150, 12, 10, 200, 300, -4, 250, 600};
    try{ System.out.println("4. " + (ProjectTwo.sumsToTarget(b, -5)==true)); } catch(Exception e){ System.out.println("failed"); }
    try{ System.out.println("5. " + (ProjectTwo.sumsToTarget(b, 1)==true)); } catch(Exception e){ System.out.println("failed"); }
    try{ System.out.println("6. " + (ProjectTwo.sumsToTarget(b, 601)==true)); } catch(Exception e){ System.out.println("failed"); }
    try{ System.out.println("7. " + (ProjectTwo.sumsToTarget(b, 0)==false)); } catch(Exception e){ System.out.println("failed"); }
    try{ System.out.println("8. " + (ProjectTwo.sumsToTarget(b, 12)==true)); } catch(Exception e){ System.out.println("failed"); }

    int[] c = {5};
    try{ System.out.println("9. " + (ProjectTwo.sumsToTarget(c, -5)==false)); } catch(Exception e){ System.out.println("failed"); }
    try{ System.out.println("10. " + (ProjectTwo.sumsToTarget(c, 5)==true)); } catch(Exception e){ System.out.println("failed"); }

这是输出:

应该总是打印出来的。

  1. 是的
  2. 假
  3. 是的
  4. 是的
  5. 是的
  6. 是的
  7. 是的
  8. 是的
  9. 是的
  10. 是的

【问题讨论】:

  • 我自己不明白这个问题。应该发生什么?
  • 使用数组中的数字添加到目标整数。

标签: java arrays recursion


【解决方案1】:

想想算法。您有一个值列表,并且您想尝试每种组合对它们求和,以查看组合是否与您的目标匹配。

假设号码是1、3 和7。所有组合将是:

      = 0
1     = 1
3     = 3
1+3   = 4
7     = 7
1+7   = 8
3+7   = 10
1+3+7 = 11

要使用递归,您基本上会取第一个数字 (1),然后尝试使用和不使用该数字,方法是对其余数字进行递归调用,到目前为止总共是 @ 987654326@或1。

在第二次调用时,您将尝试使用和不使用3。由于它的调用总数为0 或1,因此最终将总共进行4 次3 级调用,到目前为止的总数为0、@987654332 @、1、4。

等等。

如果总数等于目标,则停止递归并返回true,这应该会一直停止递归,返回调用堆栈。

可选:如果总数超过目标,您可以停止此组合的递归,因为任何进一步的递归只会离目标更远。

现在尝试实现这个逻辑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多