Py||Long integer subtraction

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/Lhw_666/article/details/102754652

题目描述
Calculate and output the difference between two long integers (not less than 0, not greater than 10 to the power of 100) to ensure that the result is non-negative.
输入
The input is a set of long integer pairs to be evaluated, each of which acts as two long integers.
输出
The difference between each pair of long integers is output, and one line is output for each difference.
样例输入 Copy
2 1
100000000 50
样例输出 Copy
1
99999950

def solution(line,line1):
    a = [int(i) for i in line ]
    b = [int(i) for i in line1 ]
    if len(a)<len(b) or (len(a)==len(b) and a[0]<b[0]):
        s=a
        a=b
        b=s
    res = ''
    for i in range(len(b)):
        flag_a = len(a)-1-i
        flag_b = len(b)-1-i
        if a[flag_a]>= b[flag_b]:
            res = str(a[flag_a]-b[flag_b])+res
        else:
            res = str(10+a[flag_a]-b[flag_b])+res
            while a[flag_a-1]==0:
                a[flag_a-1]=9
                flag_a -= 1
            a[flag_a-1] -= 1
    for j in range(len(a)-1-i-1,-1,-1):
        res = str(a[j])+res
    zero_flag=0
    for i in range(len(res)):
        if res[i]!='0':
            zero_flag=1
            break
    if zero_flag==0:
        return 0
    return res[i:]
while True:
    s=[str(i) for i in input().split()]
    s1=[]
    s2=[]
    s1=[s[0]]
    s2=[s[1]]
    print(solution(s1,s2))

Guess you like

Origin blog.csdn.net/Lhw_666/article/details/102754652