你如何递归地计算数组(Java)中负数的数量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15689097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you recursively count the number of negative numbers in an array (Java)?
提问by rphello101
I need to use this method:
我需要使用这种方法:
public static int countNegative(double[] numbers, int count){ }
to count the number of negative numbers in a double array. I could easily do it if I could include a 3rd parameter, sum, but I can only use the array and an int. I'm completely stuck. I've tried a few things, but cannot get it right. I've gotten everything from the size of the array to ArrayIndexOutOfBounds, but never the right answer. Could anyone help me out with this?
计算 double 数组中负数的数量。如果我可以包含第三个参数 sum,我可以很容易地做到这一点,但我只能使用数组和一个 int。我完全被困住了。我已经尝试了一些东西,但无法做到正确。我已经得到了从数组大小到 ArrayIndexOutOfBounds 的所有内容,但从来没有得到正确的答案。有人可以帮我解决这个问题吗?
-EDIT-
-编辑-
Well here is the exact assignment:
好吧,这是确切的分配:
Write a program that reads in a sequence of numbers (not necessary integers) from standard input until 0 is read, and stores them in an array, similar to what you did in assignment 2. This part is done using iteration . You may assume that there will not be more than 100 numbers.
Then compute the maximum number stored in the array, the count of negative numbers, and compute the sum of positive numbers, using recursion. Thus you will create recursive methods findMax, countNegative, and computeSumPositive in Assignment9 class and they will be called by a main method.
Specifically, the following recursive methods must be implemented (These method should not contain any loop):
public static double findMax(double[] numbers, int count) -> It finds the maximum number in the array, count is the number of elements
in the array
public static int countNegative(double[] numbers, int count) -> counts the negative integers
public static double computeSumPositive(double[] numbers, int count) -> sums number of positive integers
编写一个程序,从标准输入中读取一系列数字(不是必需的整数),直到读取到 0,并将它们存储在一个数组中,类似于您在赋值 2 中所做的。这部分是使用迭代完成的。您可以假设不会有超过 100 个号码。
然后计算存储在数组中的最大数,负数的计数,并使用递归计算正数的总和。因此,您将在 Assignment9 类中创建递归方法 findMax、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() was easy:
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));
}
This is my most recent attempt at countNegative. It just returns 99 (I have the array initialized with 100 elements):
这是我最近对 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);
}
I should be able to figure out the computeSumPositive if I can figure out this negative one.
如果我能找出这个负数,我应该能够计算出computeSumPositive。
Count can be whatever you need it to be. I used it more as an index in findMax.
Count 可以是任何你需要的。我更多地将它用作 findMax 中的索引。
回答by Eng.Fouad
What is the use of count
? It would make sense if it is index
:
有什么用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);
}
and call it like this:
并这样称呼它:
int count = countNegative(array, 0);
回答by rgettman
Use the int
parameter as an index into the numbers
array. Determine if the current index's value is negative (count 0 or 1 here). Then return the sum of that 0/1 count and the recursive call that looks at the next index position. The base case is when you've run past the end of the array, which returns 0.
使用int
参数作为numbers
数组的索引。确定当前索引的值是否为负(此处计数 0 或 1)。然后返回该 0/1 计数和查看下一个索引位置的递归调用的总和。基本情况是当你跑过数组的末尾时,它返回 0。
回答by Cratylus
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;
}
You call this method: countNegative(numbers, 0);
count
is to be used as the base condition of recursion. You return the result back up the stack
你调用这个方法:countNegative(numbers, 0);
count
被用作递归的基本条件。您将结果返回到堆栈中
Example:
例子:
double a[]={-12.0,1.0,0.0,23.0,-23.0,-9.0};
System.out.println(countNegative(a, 0));
I get 3
in console
我进入3
控制台
回答by RobAu
Start by implementing it for an array with 0 elemtents. The for an array of 1 element. The for an array of more,usign the previous results...
首先为具有 0 个元素的数组实现它。对于 1 个元素的数组。对于更多的数组,使用之前的结果...
回答by kMaiSmith
here's how it might work
这是它的工作原理
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;
}
You don't need the count parameter because of the way recursion works. when you call the function with an array it first determines if the first element is less than zero, making it negative. Next it checks if the array has more than one element, and if it does it calls itself with everything but the first element of the array and adds that to the result.
由于递归的工作方式,您不需要 count 参数。当您使用数组调用该函数时,它首先确定第一个元素是否小于零,使其为负。接下来,它检查数组是否有多个元素,如果有,它会使用除数组第一个元素之外的所有元素调用自己,并将其添加到结果中。
After that it returns the result which depending if it's in a recursive call or not, will either add it to the result of the call above it or give it back to the person calling it.
之后它返回结果,这取决于它是否在递归调用中,要么将其添加到其上方的调用结果中,要么将其返回给调用它的人。