题解 | 浮点数加法

浮点数加法

https://www.nowcoder.com/practice/ddec753f446e4ba4944e35378ba635c8

def add_large(a, b, a1, b1):
    a = list(map(int, a))
    a1 = list(map(int, a1))
    b = list(map(int, b))
    b1 = list(map(int, b1))

    max_ab = max(len(a), len(b))
    

    max_ab1 = max(len(a1), len(b1))
    a1 = a1[::-1]
    b1 = b1[::-1]

    a.extend([0]*(max_ab - len(a)))
    b.extend([0]*(max_ab - len(b)))
    a = a[::-1]
    b = b[::-1]


    a1.extend([0]*(max_ab1 - len(a1)))
    b1.extend([0]*(max_ab1 - len(b1)))


    carry = 0
    res = []
    carry1 = 0
    res1 = []

    for i in range(max_ab):
        res.append((a[i] + b[i] + carry) % 10)
        carry = (a[i] + b[i] + carry) // 10

    for i in range(max_ab1):
        if carry == 1:
            carry1 = 1
            carry = 0
        res1.append((a1[i] + b1[i] + carry1) % 10)
        carry1 = (a1[i] + b1[i] + carry1) // 10
    if carry1 == 1:
        res1.append(1)

    res = res[::-1]
    while len(res) > 1 and res[-1] == 0:
        res.pop()
    while len(res1) > 1 and res1[-1] == 0:
        res1.pop()
    s1 = ''.join(map(str, res1[::-1]))
    s2 = ''.join(map(str, res))
    print('{}.{}'.format(s1, s2))

a, b = input().split('.')
a1, b1 = input().split('.')
s1 = add_large(list(b), list(b1), list(a), list(a1))

全部评论

相关推荐

kl_我是东山啊:《相关公司:阿里巴巴》
投递阿里巴巴等公司10个岗位
点赞 评论 收藏
分享
MingoTree:看不出你你的技术栈,想找什么工作,然后课设项目别写上去了,自我评价删了,前后端你想好你要干啥,这种简历投上去秒挂的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务