Java 计算测井曲线的递归方法

Java 计算测井曲线的递归方法,java,recursion,Java,Recursion,为了计算递归对数,我做了如下操作:(b是log的基) 但是,这是错误的。我是在openDSA交互平台上进行的。最初的问题如下: For function "log", write the missing base case condition and the recursive call. This function computes the log of "n" to the base "b". As an example: log 8 to the base 2 equals 3 s

为了计算递归对数,我做了如下操作:(b是log的基)

但是,这是错误的。我是在openDSA交互平台上进行的。最初的问题如下:

    For function "log", write the missing base case condition and the recursive call. This function computes the log of "n" to the base "b". As an example: log 8 to the base 2 equals 3 since 8 = 2*2*2. We can find this by dividing 8 by 2 until we reach 1, and we count the number of divisions we made. You should assume that "n" is exactly "b" to some integer power.

1
int log(int b, int n ) {
2
  if <<Missing base case condition>> {
3
    return 1;
4
  } else {
5
    return <<Missing a Recursive case action>>
6
  }
7
}
对于函数“log”,编写缺少的基本情况条件和递归调用。此函数将“n”的日志计算到基“b”。例如:由于8=2*2*2,所以基数2的对数8等于3。我们可以用8除以2,直到达到1,然后计算我们所做的除法的数量。您应该假设“n”正好是某个整数幂的“b”。
1.
整数日志(整数b,整数n){
2.
如果{
3.
返回1;
4.
}否则{
5.
返回
6.
}
7.
}

我的代码不正确。我得到了无限递归

如果格式必须如下所示:

int log(int b, int n ) {
    if <<enter base case>> {
        return 1;
    } else {
        return <<enter action case>> ;
    }
}
int日志(int b,int n){
如果{
返回1;
}否则{
返回;
}
}
那么我能想到的最安全的方法是:

int log(int b, int n ) {
    if (n <= b) {
        return 1;
    } else {
        return log(b, n/b)+1 ;

    }
}
int日志(int b,int n){

如果(n我认为更好的解决方案是

int log(int b, int n ) {
    if (b > n) {
        return 0;
    } else {
        return 1 + log(b, n/b);
    }
}
这将返回n的对数基数b,舍入为整数,并且与其他答案相比,与不精确结果具有更高的一致性

e、 对数(2,6)=2;对数(2,7)=2;对数(2,8)=3;对数(2,9)=3


尽管如此,它仍然不能处理诸如b<2或结果为负的情况。

您遇到的问题是什么?问题是因为您有
返回日志(int b,int n/b)+1;
而不是
返回日志(b,n/b)+1、 
?使用整数进行此计算有什么意义?我添加了openDSA的完整问题,请查看我编辑的问题。是的,但如果是17的日志基数2,预期输出是什么?我可能缺少一些内容,但看起来不正确。log(2,6)、log(2,7)、log(2,8)、log(2,9)使用log函数返回3。请参阅@AmirGonnen“您应该假设“n”正好是某个整数幂的“b”。--从问题中可以看出。您是对的,最后一句有点误导性……删除。
int log(int b, int n ) {
    if (b > n) {
        return 0;
    } else {
        return 1 + log(b, n/b);
    }
}