90分难死了QwQ
  • 板块P1001 A+B Problem
  • 楼主QAQ5
  • 当前回复6
  • 已保存回复6
  • 发布时间2024/7/24 07:35
  • 上次更新2024/7/24 09:21:40
查看原帖
90分难死了QwQ
749028
QAQ5楼主2024/7/24 07:35
# 文字加法器
def             add             (numStr1,numStr2):
    l1 = len(numStr1)
    l2 = len(numStr2)
    if l1 == 0 or l2 == 0:
        return
    if l1 == l2 and numStr1[0] != '-' and numStr2[0] != '-':
        bottomRow = [0 for count in range(0,l1 + 1)]
        endIdx = l1 - 1
        while endIdx >= 0:
            oneAdd = int(numStr1[endIdx]) + int(numStr2[endIdx])
            bottomRow[endIdx + 1] += oneAdd % 10
            if oneAdd > 9:
                oneAdd = int(oneAdd / 10)
                bottomRow[endIdx] = oneAdd
            endIdx -= 1
        if bottomRow[0] == 0:
            bottomRow = [str(n) for n in bottomRow[1:]]
        else:
            bottomRow = [str(n) for n in bottomRow]
        resultStr = ''.join(bottomRow)
        return resultStr
    elif l1 == l2 and numStr1[0] == '-' and numStr2[0] == '-':
        resultStr = add(numStr1[1:],numStr2[1:])
        resultStr = '-' + resultStr
        return resultStr
    elif l1 > l2 and numStr1[0] != '-' and numStr2[0] != '-':
        sub = l1 - l2
        numStr2 = list(numStr2)
        numStr2 = ['0']*sub + numStr2
        numStr2 = ''.join(numStr2)
        endIdx = l1 - 1
        bottomRow = [0 for count in range(0,l1 + 1)]
        while endIdx >= sub:
            oneAdd = int(numStr1[endIdx]) + int(numStr2[endIdx])
            bottomRow[endIdx + 1] += oneAdd % 10
            if oneAdd > 9:
                oneAdd = int(oneAdd / 10)
                bottomRow[endIdx] += oneAdd
            endIdx -= 1
        while endIdx >= 0:
            oneAdd = bottomRow[endIdx + 1] + int(numStr1[endIdx])
            bottomRow[endIdx + 1] = oneAdd % 10
            if oneAdd > 9:
                oneAdd = int(oneAdd / 10)
                bottomRow[endIdx] += oneAdd
            else:
                while endIdx >= 0:
                    bottomRow[endIdx + 1] = numStr1[endIdx]
                    endIdx -= 1
                continue
            endIdx -= 1
        if bottomRow[0] == 0:
            bottomRow = bottomRow[1:]
        bottomRow = [str(n) for n in bottomRow]
        resultStr = ''.join(bottomRow)
        return resultStr
    elif l1 < l2 and numStr1[0] != '-' and numStr2[0] != '-':
        return add(numStr2,numStr1)
    elif l1 > l2 and numStr1[0] == '-' and numStr2[0] == '-':
        return '-' + add(numStr1[1:],numStr2[1:])
    elif l1 < l2 and numStr1[0] == '-' and numStr2[0] == '-':
        return '-' + add(numStr1[1:],numStr2[1:])
    elif l1 > l2 - 1 and numStr1[0] != '-' and numStr2[0] == '-':
        numStr2 = numStr2[1:]
        l2 = len(numStr2)
        sub = l1 - l2
        numStr2 = list(numStr2)
        numStr2 = ['0']*sub + numStr2
        numStr2 = ''.join(numStr2)
        endIdx = l1 - 1
        bottomRow = [0 for count in range(0,l1)]
        while endIdx >= sub:
            oneSub = int(numStr1[endIdx]) - int(numStr2[endIdx])
            if oneSub >= 0:
                bottomRow[endIdx] += oneSub
            else:
                oneSub = 10 + oneSub
                bottomRow[endIdx] += oneSub
                endi = endIdx - 1
                numStr1 = list(numStr1)
                while numStr1[endi] == '0':
                    numStr1[endi] = '9'
                    endi -= 1
                else:
                    numStr1[endi] = str(int(numStr1[endi]) - 1)
                numStr1 = ''.join(numStr1)
            endIdx -= 1
        while endIdx >= 0:
            if numStr1[endIdx] != 0:
                bottomRow[endIdx] += int(numStr1[endIdx])
            endIdx -= 1
        cutTo = 0
        for n in bottomRow:
            if n == 0:
                cutTo += 1
            else:
                break
        bottomRow = bottomRow[cutTo:]
        resultStr = [str(n) for n in bottomRow]
        resultStr = ''.join(resultStr)
        return resultStr
    elif l1 - 1 < l2 and numStr1[0] == '-' and numStr2[0] != '-':
        return add(numStr2,numStr1)
    elif l1 == (l2 - 1) and numStr1[0] != '-' and numStr2[0] == '-':
        numStr2 = numStr2[1:]
        # 。。。唉
        return int(numStr1) - int(numStr2)
    elif (l1 - 1) == l2 and numStr1[0] == '-' and numStr2[0] != '-' and False:
        return int(numStr1) + int(numStr2)# ???
    elif str(int(numStr1)) != numStr1 and str(int(numStr2)) != numStr2:
        return int(numStr1) + int(numStr2)# 一百
    elif False:
        return int(numStr1) + int(numStr2)# 还是qwq
    else:
        return int(numStr1) % int(numStr2)# 写不下也太慢QAQ

two = input().split(' ')
print(add(two[0],two[1]))
2024/7/24 07:35
加载中...