有两个字符串类型的数字,实现一个方法将它们进行相加,并返回相加后的数值。


def sum_str(a: str, b: str) -> str:
    max_str, min_str = (a, b) if len(a) > len(b) else (b, a)
    c = 0
    tmp_num = False
    for i in range(len(min_str)):
        d = int(max_str[-(i + 1)]) + int(min_str[-(i + 1)])
        d = d + 1 if tmp_num else d
        tmp_num = False
        if d > 10:
            tmp_num = True
        c += d if i == 0 else d * 10 ** i
    for i in range(len(min_str), len(max_str)):
        if tmp_num:
            c += int((max_str[-(i + 1)] + 1)) * 10 ** i
            tmp_num = False
        c += int(max_str[-(i + 1)]) * 10 ** i
    return c

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM