题解 | #高精度整数加法#
高精度整数加法
https://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6
# s1 = int(input()) # s2 = int(input()) # print(s1+s2) s1 = input() s2 = input() s1_stack = [] s2_stack = [] flag = 0 res = [] for i in s1: s1_stack.append(i) for i in s2: s2_stack.append(i) while(s1_stack and s2_stack): s1_a = s1_stack.pop() s2_a = s2_stack.pop() if flag == 0: s12 = int(s1_a) + int(s2_a) else: s12 = int(s1_a) + int(s2_a)+1 if s12 <10: res.append(str(s12)) flag = 0 else: res.append(str(s12%10)) flag = 1 while (flag == 1): if s1_stack: ans = int(s1_stack.pop())+1 if ans < 10: res.append(str(ans)) flag = 0 else: res.append(str(ans%10)) elif s2_stack: ans = int(s2_stack.pop())+1 if ans < 10: res.append(str(ans)) flag = 0 else: res.append(str(ans%10)) else: res.append('1') flag = 0 if s1_stack: res += s1_stack[::-1] elif s2_stack: res += s2_stack[::-1] print(''.join(res[::-1]))一种是直接利用python大整数计算直接输出,另外一种就是按位,从后往前相加