折叠

PAT乙级-1019 数字黑洞

 

 

 

解题代码(部分非零返回14分):

N = input()

N_list = []

for i in N:
    N_list.append(int(i))

Kaprekar_is = 0
if N_list[0]==N_list[1] and N_list[0]==N_list[2] and N_list[0]==N_list[3]:
    N_list_str = ''.join(map(str,N_list))
    print(N_list_str+" - "+N_list_str+" = 0000")
else:
    while(Kaprekar_is!=6174):
        #非递增排序
        N_list_down = sorted(N_list,reverse = True)
        #非递减排序
        N_list_up = sorted(N_list)
        N_down_int = int(''.join(map(str,N_list_down)))
        N_up_int = int(''.join(map(str,N_list_up)))
        N_list_int = N_down_int - N_up_int
        N_list = []
        for i in str(N_list_int):
            N_list.append(i)
        N_down_str = ''.join(map(str,N_list_down))
        N_up_str = ''.join(map(str,N_list_up))
        N_list_str = ''.join(N_list)
        Kaprekar_is = int(N_list_str)
        print(N_down_str+" - "+N_up_str+" = "+str(N_list_int))

exit(0)

 

posted @ 2021-11-24 16:07  Coverpast  阅读(33)  评论(0编辑  收藏  举报