题解 | 取近似值
取近似值
https://www.nowcoder.com/practice/3ab09737afb645cc82c35d56a5ce802a
import math x = float(input()) down = math.floor(x) up = math.ceil(x) if up - x <= x - down: print(up) else: print(down)
- 对于python中的小数/浮点数,数据类型是float,没有double
- 导入math库,使用math.ceil()完成向上取整,使用math.floor()完成向下取整
- 通过上下取整后与原数的差值进行比较,偏向小于的一方,如果等于选择上取整