1019(有三个测试点错误)

def max_num(num):
    for y in range(1, len(num)):
        for i in range(0, len(num)-y):
            if num[i] < num[i+1]:
                num[i] = num[i] + num[i+1]
                num[i+1] = num[i] - num[i+1]
                num[i] = num[i] - num[i+1]
    Max = 0
    for i in range(0, len(num)):
        Max += num[i] * 10 ** (len(num) - 1 - i)
    return Max
def min_num(num):
    for y in range(1, len(num)):
        for i in range(0, len(num)-y):
            if num[i] > num[i+1]:
                num[i] = num[i] + num[i+1]
                num[i+1] = num[i] - num[i+1]
                num[i] = num[i] - num[i+1]
    Min = 0
    for i in range(0, len(num)):
        Min += num[i] * 10 ** (len(num) - 1 - i)
    return Min
    
def printf(n):
    if n > 999:
        print(n, end = '')
    elif n > 99:
        print("0%d" %n, end = '')
    elif n > 9:
        print("00%d" %n, end = '')
    else:
        print("000%d" %n, end = '')
num = input()
while True:
    num = list(map(int, num))
    Max = max_num(num)
    Min = min_num(num)
    N = Max - Min
    printf(Max)
    print(' - ', end = '')
    printf(Min)
    print(' = ', end = '')
    printf(N)
    print()
    num = list(str(N))
    if N < 1000:
        t = '0' + str(N)
        num = list(t)
    else:
        num = list(str(N))
    if N == 6174 or N == 0:
        break
    
    
 

你可能感兴趣的:(PAT)