题目
添加两个基于X的对数(㏒),其值为A和B.示例log(X) A + log(X) B,其中底数为X.
测试用例:
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class EasyLogsExampleTests {
@Test
public void exampleTests() {
assertEquals(0.7781512503836435, EasyLogs.logs(10, 2, 3), 0.0000001);
assertEquals(1.1132827525593785, EasyLogs.logs(5, 2, 3), 0.0000001);
assertEquals(0.25938375012788123, EasyLogs.logs(1000, 2, 3), 0.0000001);
}
}
解题
My
public class EasyLogs {
public static double logs(double x, double a, double b) {
return Math.log(a) / Math.log(x) + Math.log(b) / Math.log(x);
}
}
Other
public class EasyLogs {
public static double logs(double x, double a, double b) {
return (Math.log(a) + Math.log(b)) / Math.log(x);
}
}
后记
这题的原题是英文的,想了半天都不明白log这个日志是啥,还有提供的测试用例是怎么算的,半小时后,我想明白了,log是数学里的对数o(╯□╰)o。
网友评论