PAT digital black hole

Given any one of the digits not identical four positive integer, if we first four digits ordered by nonincreasing, then a non-descending order, and then the first number minus the second number, will get a new digital. Has been repeated so doing, we will soon be parked in the "digital black hole," said the 6174, the magic number is also called Kaprekar constant.

For example, we start from 6767, will be:

7766 - 6677 = 1089

9810 - 0189 = 9621

9621 - 1269 = 8352

8532 - 2358 = 6174

7641 - 1467 = 6174

... ...

Now given any four positive integers, write a program demonstrates the process of reaching the black hole.

Input formats:

A given input  (0,10 4 positive integer) of section N.

Output formats:

If N is equal to 4-bit digital full, the output in line N - N = 0000; otherwise, the output of each step will be calculated in a row, as a difference occurs until 6174, see sample output format. Note that each of the 4 bits output in digital format.

Sample Input 1:

6767

Output Sample 1:

7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174

Sample Input 2:

2222

Output Sample 2:

2222 - 2222 = 0000
a=int(input())
f=[]
if a==1111 or a==3333 or a==2222 or a==4444 or a==5555 or a==6666 or a==7777 or a==8888 or a==9999:
    print('%04d - %04d = 0000'%(a,a))
elif a==6174:
    print('7641 - 1467 = 6174')
else:
    while a!=6174:
        b=a%10
        c=a//10%10
        d=a//100%10
        e=a//1000%10
        f.append(b)
        f.append(c)
        f.append(d)
        f.append(e)
        f.sort()
        m=f[3]*1000+f[2]*100+f[1]*10+f[0]
        k=f[0]*1000+f[1]*100+f[2]*10+f[3]
        a=m-k
        print('%04d - %04d = %04d'%(m,k,a))
        f.clear()

This question needs to be noted is that when direct input is 6174, when the output of one operation or to reach the 6174 process, that is, direct output 7641--1467 = 6174

Guess you like

Origin www.cnblogs.com/andrew3/p/12636467.html