python下大数相加

发布时间:2019-09-16 07:20:18编辑:auto阅读(1562)

    s1=input('please enter number:')
    s2=input('please enter number:')
    L1=[0]
    L2=[0]
    for i in range(0,len(s1)):
    	L1.append(int(s1[i]))
    for i in range(0,len(s2)):
    	L2.append(int(s2[i]))
    if(len(s1)>len(s2)):
     for i in range(len(s1)-len(s2)):
        L2.append(0)
    elif(len(s1)<=len(s2)):
    	for i in range(len(s2)-len(s1)):
    		L1.append(0)
    for i in range(len(L1)):
            L1[i]=L1[i]+L2[i]
    A=B=len(L1)-1
    while A>0:
     	if((L1[A])/10)>=1:  # or if((L1[A]//10)>0)
     		L1[A]=L1[A]%10
     		L1[A-1]=L1[A-1]+1
     	A-=1
    if L1[0]==0:
            for i in range(1,B+1):
                    print(L1[i],end='')
    elif L1[0]!=0:
            for i in range(B+1):
                    print(L1[i],end='')

    在Python下写一个大数相加的小程序


关键字