赞
踩
二进制数转字符串
给定一个介于0和1之间的实数(如0.72),类型为double,打印它的二进制表达式。如果该数字无法精确地用32位以内的二进制表示,则打印“ERROR”。
示例1:
输入:0.625
输出:“0.101”
示例2:
输入:0.1
输出:“ERROR”
提示:0.1无法被二进制准确表示
提示:
32位包括输出中的 “0.” 这两位。
题目保证输入用例的小数位数最多只有 6 位
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/bianry-number-to-string-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution:
def printBin(self, num: float) -> str:
ans = "0."
now = 1
for i in range(32):
now /= 2
ans += str(int(num / now))
if not (num := num % now):
# 海象运算符,用于赋值后判断
return ans
return "ERROR"
以上,共勉
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。