← 返回题库
初级

数字黑洞

未完成
初级参考 完整示例代码供参考,建议自己理解后重新输入
def solve(n):
    while True:
        s = str(n).zfill(4)
        if len(set(s)) == 1:
            print(f'{s} - {s} = 0000')
            break
        big = int(''.join(sorted(s, reverse=True)))
        small = int(''.join(sorted(s)))
        n = big - small
        print(f'{big:04d} - {small:04d} = {n:04d}')
        if n == 6174:
            break

示例

输入
solve(6767)
期望输出
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码