题解 | #高精度整数加法#

高精度整数加法

https://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6

s1 = input().strip()
s2 = input().strip()

if len(s1) < len(s2):
    s1, s2 = s2, s1
s2 = s2.rjust(len(s1), '0')
res = []

# 进位
carry = 0

for i, j in zip(s1[::-1], s2[::-1]):
    num = int(i) + int(j) + carry
    single = num % 10
    carry = num // 10
    res.append(str(single))

# 最后不等于 0则加上去
if carry != 0:
    res.append(str(carry))

print(''.join(res[::-1]))
全部评论

相关推荐

自由水:笑死了,敢这么面试不敢让别人说
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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