题目
你的工作是找到两个球形物体(obj1,obj2)之间的引力。
输入
给出两个数组:
arr_val(值数组)由3个元素组成
元素1:obj 1的质量
元素2:obj 2的质量
元素3:中心之间的距离
arr_unit(单元数组)由3个元素组成
元素1:obj 1的质量单位
元素2:obj 2的质量单位
元素3:中心之间距离的单位
质量单位是:
千克(kg)
克(g)
毫克(mg)
微克(μg)
磅(lb)
距离单位是:
米(m)
厘米(cm)
毫米(mm)
千分尺(μm)
英尺(ft)
注意
值G = 6.67 x 10-11Nkg–2m2
1英尺 = 0.3048米
1磅 = 0.453592千克
返回值必须符合牛顿定律(显然)
μ
从此处复制此内容以在解决方案中使用它。
测试用例:
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class SampleTestCases {
private static void test(double expected, double[] arrVal, String[] arrUnit) {
assertEquals(expected, Solution.solution(arrVal, arrUnit), expected * 1e-5);
}
@Test
public void sampleTests() {
test(6.67e-12, new double[] {1000, 1000, 100}, new String[] {"g", "kg", "m"});
test(6.67e-9, new double[] {1000, 1000, 100}, new String[] {"kg", "kg", "m"});
test(0.0000667, new double[] {1000, 1000, 100}, new String[] {"kg", "kg", "cm"});
}
}
解题
My
思路:判断单位后换算标准单位再计算,感觉好长,看看其他人有没有啥好方法。
public class Solution {
public static double solution(double[] arrVal, String[] arrUnit) {
final double G = 6.67e-11;
if ("g".equals(arrUnit[0])) {
arrVal[0] *= 1e-3;
} else if ("mg".equals(arrUnit[0])) {
arrVal[0] *= 1e-6;
} else if ("μg".equals(arrUnit[0])) {
arrVal[0] *= 1e-9;
} else if ("lb".equals(arrUnit[0])) {
arrVal[0] *= 0.453592;
}
if ("g".equals(arrUnit[1])) {
arrVal[0] *= 1e-3;
} else if ("mg".equals(arrUnit[1])) {
arrVal[1] *= 1e-6;
} else if ("μg".equals(arrUnit[1])) {
arrVal[1] *= 1e-9;
} else if ("lb".equals(arrUnit[1])) {
arrVal[1] *= 0.453592;
}
if ("cm".equals(arrUnit[2])) {
arrVal[2] *= 1e-2;
} else if ("mm".equals(arrUnit[2])) {
arrVal[2] *= 1e-3;
} else if ("μm".equals(arrUnit[2])) {
arrVal[2] *= 1e-6;
} else if ("ft".equals(arrUnit[2])) {
arrVal[2] *= 0.3048;
}
return G * arrVal[0] * arrVal[1] / (arrVal[2] * arrVal[2]);
}
}
Other
我发现挺简洁的两个。
import java.util.HashMap;
import java.util.Map;
public class Solution {
private final static Double G = 6.67e-11;
public static double solution(double[] arrVal, String[] arrUnit) {
Map<String, Double> convertions = new HashMap<>();
convertions.put("kg", 1.0);
convertions.put("g", 1e-3);
convertions.put("mg", 1e-6);
convertions.put("μg", 1e-9);
convertions.put("lb", 0.453592);
convertions.put("m", 1.0);
convertions.put("cm", 1e-2);
convertions.put("mm", 1e-3);
convertions.put("μm", 1e-6);
convertions.put("ft", 0.3048);
double m1 = arrVal[0] * convertions.get(arrUnit[0]);
double m2 = arrVal[1] * convertions.get(arrUnit[1]);
double r = arrVal[2] * convertions.get(arrUnit[2]);
return G * m1 * m2 / r / r;
}
}
import java.util.*;
public class Solution {
public static double solution(double[] arrVal, String[] arrUnit) {
Map<String, Double>massUnits = new HashMap<>();
massUnits.put("kg",1d);
massUnits.put("g",1/1000d);
massUnits.put("mg",1/1000000d);
massUnits.put("μg",1/1000000000d);
massUnits.put("lb",0.453592);
Map<String, Double>distancenits = new HashMap<>();
distancenits.put("m",1d);
distancenits.put("cm",1/100d);
distancenits.put("mm",1/1000d);
distancenits.put("μm",1/1000000d);
distancenits.put("ft",0.3048);
return 6.67e-11*(arrVal[0]*massUnits.get(arrUnit[0])*arrVal[1]*massUnits.get(arrUnit[1]))/(arrVal[2]*arrVal[2]*distancenits.get(arrUnit[2])*distancenits.get(arrUnit[2]));
}
}
我发现挺复杂的解法o(╯□╰)o
public class Solution {
private static final double KG_TO_LB = 1.0d / 0.453592d;
private static final double LB_TO_KG = 0.453592d;
private static final double M_TO_FT = 1.0d / 0.3048d;
private static final double FT_TO_M = 0.3048d;
private static final double GRAVITATIONAL_FORCE = 6.67d * Math.pow(10, -11);
public static double solution(double[] arrVal, String[] arrUnit) {
double m1 = massConvertToUnit(arrVal[0], arrUnit[0], "kg"), m2 = massConvertToUnit(arrVal[1], arrUnit[1], "kg");
double r = distanceConvertToUnit(arrVal[2], arrUnit[2], "m");
return GRAVITATIONAL_FORCE * ((m1 * m2) / Math.pow(r, 2));
}
private static double massConvertToUnit(double value, String fromUnit, String toUnit) {
if(fromUnit.equals(toUnit))
return value;
return value * getMassConvertNumber(fromUnit, toUnit);
}
private static double distanceConvertToUnit(double value, String fromUnit, String toUnit) {
if(fromUnit.equals(toUnit))
return value;
return value * getDistanceConvertNumber(fromUnit, toUnit);
}
private static double getMassConvertNumber(String fromUnit, String toUnit) {
switch(fromUnit) {
case "kg":
switch(toUnit) {
case "g":
return 1_000.0d;
case "mg":
return 1_000_000.0d;
case "μg":
return 1_000_000_000.0d;
case "lb":
return KG_TO_LB;
}
case "g":
switch(toUnit) {
case "kg":
return 1.0d / 1_000.0d;
case "mg":
return 1_000.0d;
case "μg":
return 1_000_000.0d;
case "lb":
return KG_TO_LB * (1.0d / 1_000.0d);
}
case "mg":
switch(toUnit) {
case "kg":
return 1.0d / 1_000_000.0d;
case "g":
return 1.0d / 1_000.0d;
case "μg":
return 1_000.0d;
case "lb":
return KG_TO_LB * (1.0d / 1_000_000.0d);
}
case "μg":
switch(toUnit) {
case "kg":
return 1.0d / 1_000_000_000.0d;
case "g":
return 1.0d / 1_000_000.0d;
case "mg":
return 1_000_000.0d;
case "lb":
return KG_TO_LB * (1.0d / 1_000_000_000.0d);
}
case "lb":
switch(toUnit) {
case "kg":
return LB_TO_KG;
case "g":
return LB_TO_KG / 1_000.0d;
case "mg":
return LB_TO_KG / 1_000_000.0d;
case "μg":
return LB_TO_KG / 1_000_000_000.0d;
}
}
return 1.0d;
}
private static double getDistanceConvertNumber(String fromUnit, String toUnit) {
switch(fromUnit) {
case "m":
switch(toUnit) {
case "cm":
return 100.0d;
case "mm":
return 1_000.0d;
case "μm":
return 1_000_000.0d;
case "ft":
return M_TO_FT;
}
case "cm":
switch(toUnit) {
case "m":
return 1.0d / 100.0d;
case "mm":
return 10.0d;
case "μm":
return 10_000.0d;
case "ft":
return M_TO_FT * (1.0d / 100.0d);
}
case "mm":
switch(toUnit) {
case "m":
return 1.0d / 1_000.0d;
case "cm":
return 1.0d / 10.0d;
case "μm":
return 1_000.0d;
case "ft":
return M_TO_FT * (1.0d / 1_000.0d);
}
case "μm":
switch(toUnit) {
case "m":
return 1.0d / 1_000_000.0d;
case "cm":
return 1.0d / 10_000.0d;
case "mm":
return 1.0d / 1_000.0d;
case "ft":
return M_TO_FT * (1.0d / 1_000_000.0d);
}
case "ft":
switch(toUnit) {
case "m":
return FT_TO_M;
case "cm":
return FT_TO_M * (1.0d / 100.0);
case "mm":
return FT_TO_M * (1.0d / 1_000.0);
case "μm":
return FT_TO_M * (1.0d / 1_000_000.0);
}
}
return 1.0d;
}
}
还有用switch简单点的解法:
public class Solution {
public static double solution(double[] arrVal, String[] arrUnit) {
double mass1 = arrVal[0] * massUnit(arrUnit[0]);
double mass2 = arrVal[1] * massUnit(arrUnit[1]);
double distance = arrVal[2] * distanceUnit(arrUnit[2]);
double force = G * mass1 * mass2 / distance / distance;
return force;
}
static final double G = 6.67e-11;
static double massUnit(String unit) {
switch (unit) {
case "μg": return 1e-9;
case "mg": return 1e-6;
case "g": return 1e-3;
case "kg": return 1.0;
case "lb": return 0.453592;
default:
throw new IllegalArgumentException("Unknown mass unit: " + unit);
}
}
static double distanceUnit(String unit) {
switch (unit) {
case "μm": return 1e-6;
case "mm": return 1e-3;
case "cm": return 1e-2;
case "m": return 1.0;
case "ft": return 0.3048;
default:
throw new IllegalArgumentException("Unknown distance unit: "
+ unit);
}
}
}
后记
这题总结下有if、switch还是map解题思路。