面试题 05.02. 二进制数转字符串

二进制数转字符串。给定一个介于0和1之间的实数(如0.72),类型为double,打印它的二进制表达式。如果该数字不在0和1之间,或者无法精确地用32位以内的二进制表示,则打印“ERROR”。

示例1:

输入:0.625
输出:"0.101"
示例2:

输入:0.1
输出:"ERROR"
提示:0.1无法被二进制准确表示
提示:

32位包括输出中的"0."这两位。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/bianry-number-to-string-lcci

class Solution:
    def printBin(self, num: float) -> str:
        res,a='0.',32
        while num>0 and a:
            num*=2
            if num>=1:
                res+='1'
                num-=1
            else:
                res+='0'
            a-=1
        return res if not num else 'ERROR'
原文地址:https://www.cnblogs.com/xxxsans/p/13700788.html