尝试在Java中使用数学公式计算分数。

huangapple go评论68阅读模式
英文:

Trying to calculate a score using a math formula in Java

问题

我正在尝试在Java中使用一个公式计算分数。我在将其翻译为Java代码方面遇到了问题。

公式:𝑠𝑐𝑜𝑟𝑒 = 3 ^ ( ( log2( points/3) ) + 1)

其中 points 为输入值。

我目前有以下代码片段,但是我得不到期望的结果:

int score = 3 ^ (int)(Math.pow(Math.log(12 / 3), 2) + 1);

有人能告诉我我做错了什么,并可能给我一个正确的片段吗?以输入值 12 为例,结果应为 27。非常感谢。

编辑:

输入值 3 = 3
输入值 6 = 9
输入值 12 = 27
输入值 24 = 81
输入值 48 = 243
输入值 96 = 729
输入值 192 = 2187
等等
英文:

I am trying to calculate a score in Java using a formula. I am having trouble with translating it to Java code.

Formula: 𝑠𝑐𝑜𝑟𝑒 = 3 ^ ( ( log2( points/3) ) + 1)

points = the input.

I currently have this piece of code, but I am not getting the result I would like:

int score = 3 ^ (int)(Math.pow(Math.log(12 / 3), 2) + 1);

Could anyone tell me what I am doing wrong and possibly give me a snippet of how it should be? The result should be 27 with 12 as input. Thank you very much in advance.

Edit:

Input 3 = 3
Input 6 = 9
Input 12 = 27
Input 24 = 81
Input 48 = 243
Input 96 = 729
Input 192 = 2187
etc.

答案1

得分: 2

公式为:

int score = (int) Math.pow(3, Math.log(points/3.0) / Math.log(2) + 1);
  • 在Java中,^ 是按位异或操作符,您需要使用 Math.pow 来进行幂运算。
  • log2(x) 表示以 2 为底的对数,没有内置的函数,但您可以使用 log(x)/log(2) 来计算它。

如果输出稍有偏差,您可能希望使用 Math.round 而不是强制转换为 (int)

英文:

The formula is:

int score = (int) Math.pow(3, Math.log(points/3.0) / Math.log(2) + 1);
  • ^ in Java is the bit-wise XOR operator, you need to use Math.pow instead for power
  • log2(x) refers to "base 2" logarithm, there's no builtin function for it but you can you can compute it with log(x)/log(2)

You may want to use Math.round instead of cast to (int) in case the output is slightly off.

答案2

得分: 1

这段代码中存在多个错误,主要的错误是您在幂运算中使用了^运算符,而^运算符是用于“按位异或”的。

另一个错误是您将Math.log()用作以e为底的对数,尽管我们需要以2为底的对数。

现在让我们重新编写这段代码:

首先,我们要获取以2为底的n的对数,为此,我们使用以下公式:
> logb(n) = loge(n) / loge(b)

因此,要获取12/3的以2为底的对数,我们可以写成:
Math.log(12/3)/Math.log(2)

现在,获取幂次的其余操作很容易:
(int) (Math.log(12/3) / Math.log(2) + 1)

最后一部分是正确使用Math.pow()函数,如果我们要计算3^n,我们应该输入Math.pow(3, n)

因此,最终的代码将是:Math.pow(3, (int) (Math.log(12/3) / Math.log(2) + 1) )

英文:

There are multiple mistakes in this code, the main one though is that you are using the ^ operator for power, the ^ operator for "bitwise exclusive OR",

Another mistake is that you used Math.log() as if it's in base 2, even though it's in base e.

Now let's rewrite the code again:

First we want to get log in base 2 of n, to do so, we use the formula:
> logb(n) = loge(n) / loge(b)

So to get log in base 2 of 12/3 we'd write:
Math.log(12/3)/Math.log(2)

Now the rest of the operation to get the number in the power is easy:
(int) (Math.log(12/3) / Math.log(2) + 1)

The last bit is using the Math.pow() function properly, if we want 3^n we'd need to type in Math.pow(3, n).

So the final code would be: Math.pow(3, (int) (Math.log(12/3) / Math.log(2) + 1) )

答案3

得分: 0

我已经尝试为您逐步拆分。请查看下面的代码片段:

public class Score {
    public static void main(String[] args) {
        int points = 12;
        int dividedPoints = points / 3;
        int log2Value = log2(dividedPoints);
        int k = log2Value + 1;
        int score = (int) Math.pow(3, k);
        System.out.println("final score is = " + score);
    }

    public static int log2(int N) {
        // using log() method
        int result = (int) (Math.log(N) / Math.log(2));
        System.out.println("log2 Value is = " + result);
        return result;
    }
}

它将产生以下输出:

log2 Value is = 2
final score is = 27
英文:

I have tried to split it step by step for you. Please check the snippet below.

public class Score {
    public static void main(String[] args) {
        int points = 12;
        int dividedPoints = points / 3;
        int log2Value = log2(dividedPoints);
        int k = log2Value + 1;
        int score = (int) Math.pow(3, k);
        System.out.println("final score is = " + score);
    }

    public static int log2(int N) {
        // using log() method
        int result = (int) (Math.log(N) / Math.log(2));
        System.out.println("log2 Value is = " + result);
        return result;
    }
}

It would produce the following output

log2 Value is = 2
final score is = 27

huangapple
  • 本文由 发表于 2020年8月27日 01:43:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63603030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定