【问题标题】:How do you recursively count the number of negative numbers in an array (Java)?你如何递归地计算数组中负数的数量(Java)?
【发布时间】:2013-03-19 07:40:03
【问题描述】:

我需要用这个方法:

public static int countNegative(double[] numbers, int count){ }

计算双精度数组中负数的个数。如果我可以包含第三个参数 sum,我可以很容易地做到这一点,但我只能使用数组和一个 int。我完全被困住了。我已经尝试了几件事,但无法做到正确。我已经得到了从数组大小到 ArrayIndexOutOfBounds 的所有内容,但从来没有正确的答案。谁能帮我解决这个问题?

-编辑-

这里是确切的分配:

编写一个程序,从标准输入读取一系列数字(不是必需的整数),直到读取到 0,并将它们存储在 数组,类似于你在作业2中所做的。这部分完成了 使用迭代。您可以假设不会超过 100 数字。

然后计算存储在数组中的最大个数,计数 负数,并计算正数之和,使用 递归。因此,您将创建递归方法 findMax, Assignment9 类中的 countNegative 和 computeSumPositive 以及它们 将由 main 方法调用。

具体来说,必须实现以下递归方法 (这些方法不应包含任何循环):

public static double findMax(double[] numbers, int count)  -> It finds the maximum number in the array, count is the number of elements

在数组中

public static int countNegative(double[] numbers, int count) -> 计算负整数

public static double computeSumPositive(double[] numbers, int count) -> 对正整数的个数求和

findMax() 很简单:

public static double findMax(double[] numbers, int count){
        if(numbers.length - 1 == count)
            return numbers[count];
        else 
            return Math.max(numbers[count], findMax(numbers, count+1));
    }

这是我最近对 ​​countNegative 的尝试。它只返回 99(我用 100 个元素初始化了数组):

public static int countNegative(double[] numbers, int count){
        int i=0;
        if(numbers[count]<0)
            i=1;
        if(numbers.length-1==count)
            return count;
        else
            return i+countNegative(numbers,count+1);
     }

如果我能算出这个负数,我应该能够算出 computeSumPositive。

Count 可以是您需要的任何值。我更多地将它用作 findMax 中的索引。

【问题讨论】:

  • 你必须使用递归吗?另外,count 的参数是什么?如果您引用您在问题中给出的要求,并显示您尝试过的代码的要点,这将有很大帮助。
  • 使用计数作为索引。
  • 我添加了更多代码。是的,我必须使用递归。计数是你想要的任何东西。已引用要求。添加了最近的尝试。这是“家庭作业”。是的,count 可能最好用作索引。

标签: java arrays recursion negative-number


【解决方案1】:

count 有什么用?如果是index就有意义:

public static int countNegative(double[] numbers, int index)
{
    if(index == numbers.length) return 0;
    return (numbers[index] < 0 ? 1 : 0) + countNegative(numbers, index + 1);
}

然后这样称呼它:

int count = countNegative(array, 0);

【讨论】:

    【解决方案2】:

    使用int 参数作为numbers 数组的索引。确定当前索引的值是否为负(此处为 0 或 1)。然后返回该 0/1 计数和查看下一个索引位置的递归调用的总和。基本情况是当你跑过数组的末尾时,它返回 0。

    【讨论】:

      【解决方案3】:
      public static int countNegative(double[] numbers, int count){  
         if(count == numbers.length){  
              return 0;  
          }  
          int sum = countNegative(numbers, count + 1);  
          if(numbers[count] < 0){  
                sum++;  
          }  
          return sum;  
      }
      

      您调用此方法:countNegative(numbers, 0);
      count 将用作递归的基本条件。您将结果返回到堆栈中

      例子:

      double a[]={-12.0,1.0,0.0,23.0,-23.0,-9.0};  
      System.out.println(countNegative(a, 0));  
      

      我在控制台中得到3

      【讨论】:

      • 好问题...我的意见是问题所在。稍作调整,这确实有效。谢谢。
      【解决方案4】:

      首先为具有 0 个元素的数组实现它。用于 1 个元素的数组。对于一个数组more,使用前面的结果...

      【讨论】:

        【解决方案5】:

        这就是它的工作原理

        public static int countNegative(double[] numbers){
            int result = numbers[0] < 0 ? 1 : 0;
        
            if(numbers.length > 1) {
                result += countNegative(Arrays.copyOfRange(numbers, 1, numbers.length));
            }
        
            return result;
        }
        

        由于递归的工作方式,您不需要 count 参数。当您使用数组调用该函数时,它首先确定第一个元素是否小于零,使其为负数。接下来,它检查数组是否有多个元素,如果有,它会使用数组的第一个元素以外的所有元素调用自身,并将其添加到结果中。

        之后,它返回结果,这取决于它是否在递归调用中,要么将其添加到它上面的调用结果中,要么将其返回给调用它的人。

        【讨论】:

          猜你喜欢
          • 2022-12-03
          • 2013-07-03
          • 2022-01-06
          • 2019-01-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多