python 大数求和

# encoding:utf-8
from numpy import *
x1 = raw_input()
x2 = raw_input()
# 按照从小到大存储
array1 = array([int(i) for i in x1[::-1]])
array2 = array([int(i) for i in x2[::-1]])
array3 = [0 for x in range(100)]

m = len(array1)
n = len(array2)
flag = 0
i = 0
# 计算重合部分
while i < m and i < n:
    array3[i] = (array1[i] + array2[i] + flag) % 10
    flag = (array1[i] + array2[i] + flag) / 10
    i += 1
# 计算剩余部分
while i < m:
    array3[i] = (array1[i] + flag) % 10
    flag = (array1[i] + flag) / 10
    i += 1
while i < n:
    array3[i] = (array2[i] + flag) % 10
    flag = (array2[i] + flag) / 10
    i += 1

array3[i] = flag
#  计算最后位数,最后进位,长度+1
if flag > 0:
    i += 1
num = ""
# 倒序输出
for i in array3[i-1::-1]:
    num = num + str(i)
print num

你可能感兴趣的:(算法,python)