大数相加python版本

def bigSum(inputNumList):
    retList=[]
    jinwei=0
    for item in inputNumList:
        jinwei=0
        #print(retList)
        #print("item:%s" % item)
        item=str(item)
        baseLen=len(retList)
        itemLen=len(item)
        maxLoopLen=itemLen
        minLoopLen=baseLen
        if itemLen<baseLen:
            maxLoopLen=baseLen
            minLoopLen=itemLen
        for i in range(minLoopLen):
            digit=int(item[0-i-1])
            baseDigit=retList[i]
            he=digit+baseDigit+jinwei
            #print("minLoop digit:%d\tbaseDigit:%d\tjinwei:%d\n" % (digit,baseDigit,jinwei))
            if he >= 10:
                jinwei=1
                retList[i]=(he-10)
            else:
                jinwei=0
                retList[i]=he
            #print(retList)
        if itemLen>baseLen:
            for i in range(baseLen,itemLen,1):
                digit=int(item[0-i-1])
                baseDigit=0
                #print("digit:%d\tbaseDigit:%d\tjinwei:%d\n" % (digit,baseDigit,jinwei))
                he=digit+baseDigit+jinwei
                #print("he:%d"% he)
                if he >= 10:
                    jinwei=1
                    retList.append(he-10)
                else:
                    jinwei=0
                    retList.append(he)
            if jinwei ==1:
                retList.append(jinwei)
        elif jinwei==1:
            for i in range(itemLen,baseLen,1):
                digit=0
                baseDigit=retList[i]
                he=digit+baseDigit+jinwei
                #print("minLoop digit:%d\tbaseDigit:%d\tjinwei:%d\n" % (digit,baseDigit,jinwei))
                if he >= 10:
                    jinwei=1
                    retList[i]=(he-10)
                else:
                    jinwei=0
                    retList[i]=he
                #print(retList)
            if jinwei==1:
                #print("minLoop digit:%d\tbaseDigit:%d\tjinwei:%d\n" % (0,0,jinwei))
                retList.append(jinwei)
    #print(retList)
    retList.reverse()
    return "".join([str(i) for i in retList])      

if __name__=='__main__':
    inputNumList=[1239999,32,73234948]
    print(bigSum(inputNumList))
    inputNumList=[9999,1,1,9999999999999999999999999999999999]
    print(bigSum(inputNumList))
    inputNumList=[9999,1]
    print(bigSum(inputNumList))
    inputNumList=[1,9999]
    print(bigSum(inputNumList))
    inputNumList=[1,99999,1]
    print(bigSum(inputNumList))
    inputNumList=[1,9999999999999999999999999999999999]
    print(bigSum(inputNumList))

  

posted @ 2014-01-17 17:32  唠叨阁大学士  阅读(712)  评论(2编辑  收藏  举报