Problem: 2469. 温度转换
思路
看到这道题大概就是想到了,每日一题防止老年痴呆。主要就round函数进行位数取舍。
不过round是四舍六入,五成双(python3,python2是五成单)。不过提交结果的时候并没有提示哪个用例没过,这种方式就可以了,不需要使用其他的方式保证4舍5入。估计是因为用例计算之后基本没有几率是 x.xxxxx50这种结果。
在python2.x的doc中,round()的最后写着,“Values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done away from 0.”
保留值将保留到离上一位更近的一端(四舍六入),如果距离两端一样远,则保留到离0远的一边。所以round(0.5)会近似到1,而round(-0.5)会近似到-1。
但是到了python3.x的doc中,文档变成了"values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice."
如果距离两边一样远,会保留到偶数的一边。比如round(0.5)和round(-0.5)都会保留到0,而round(1.5)会保留到2。
Code
class Solution:
def convertTemperature(self, celsius: float) -> List[float]:
kelvin = round(celsius + 273.15,5)
fah = round(celsius*1.80 + 32.00,5)
return [kelvin,fah]