题解 | #取近似值# 复制粘贴楼上提交的,很nice

取近似值

https://www.nowcoder.com/practice/3ab09737afb645cc82c35d56a5ce802a

# HJ7 取近似值
n = float(input())
res = n // 1  # 计算整数部分数值
#print(res)
tep = n - res  # 计算小数部分数值
#print(tep)
# 判断小数部分数值,并输出结果
if tep >= 0.5:
    print(int(res + 1))
else:
    print(int(res))

代码中注释,是我一般比较笨,有时候想不出来流转过程,我就会打印出来看看是啥意思,这样帮助我很快理解。

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务