1 def split (n, numberlist): 2 numberlist.clear() 3 while (n > 0): 4 mod = n % 10 5 n = int( n / 10 ) 6 numberlist.append(mod) 7 8 def bubbleSort (numlist, len): 9 for i in range(len): 10 for j in range(len-i-1): 11 if numlist[j] < numlist[j+1]: 12 tmp = numlist[j] 13 numlist[j] = numlist[j+1] 14 numlist[j+1] = tmp 15 16 def reverse(a, k): 17 for i in range(int(k/2)): 18 t = a[i] 19 a[i] = a[k-1-i] 20 a[k-1-i] = t 21 22 def combine(a, k): 23 m = a[3] + a[2] * 10 + a[1] * 100 + a[0] * 1000 24 return m 25 26 inputStr = input(‘输入一个数字完全不相同的四位数字(1000·9999)‘) 27 length = len(inputStr) 28 if length != 4: 29 print("input number is invalid") 30 exit() 31 inputNumber = eval(inputStr) 32 tarNumber = 6174 33 numberlist = [] 34 while(inputNumber != tarNumber): 35 split(inputNumber, numberlist) 36 bubbleSort(numberlist, length) 37 maxNum = combine(numberlist, length) 38 reverse(numberlist, length) 39 minNum = combine(numberlist, length) 40 inputNumber = maxNum - minNum 41 print(‘%d-%d=%d/n‘ % (maxNum, minNum, inputNumber))
任意给一个数字不全相同的n位数,经有限次“重排求差”操作(“重排求差”操作即把该数的数字重排后得到的最大数减去重排后得到的最小数)最终的运算结果总会得到同一或同一些n位整数,这类整数好像所有n位整数的核心一样,具有很强的磁性,能够吸引其它n位整数,故这类整数为磁力数。
时间: 2024-11-05 09:27:58