整个问题: 给定一个索引从 1 到 n 且包含整数的数组 A,确定 A[1 到 n] 中有多少个整数大于 A[1]
到目前为止我所写的内容:
static int numGreater(int A[], int n, int count)
{
if(A[1] == A[n])
{
return count;
}
else
{
count = (A[1] < A[n] ? count : + 1);
return numGreater(A, n - 1, count);
}
}
有点被难住了。对于递归编码来说还是新手。
最佳答案
我终于明白了。问题是,我知道有一种方法可以不必在参数上添加“计数”,但我不知道该怎么做。我所做的是。
static int numGreater(int A[], int n, int count)
{
if (n <= 1)
{
return count;
}
else
{
count = A[1] < A[n] ? count + 1 : count;
return numGreater(A, n - 1, count);
}
}
我很确定有更好的方法可以做到这一点,并且我仍然想了解它们以拓宽我对递归的理解。如果有进一步的答案,我们将不胜感激。
关于Java递归: Determine how many numbers in an array is greater than index 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39193795/