Love丶FFC's Blog
给定任一个各位数字不完全相同的 4 位正整数,如果我们先把 4 个数字按非递增排序,再按非递减排序,然后用第 1 个数字减第 2 个数字,将得到一个新的数字。一直重复这样做,我们很快会停在有“数字黑洞”之称的 6174
,这个神奇的数字也叫 Kaprekar 常数。
例如,我们从6767
开始,将得到
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
... ...
现给定任意 4 位正整数,请编写程序演示到达黑洞的过程。
输入给出一个 (0,104) 区间内的正整数 N。
如果 N 的 4 位数字全相等,则在一行内输出 N - N = 0000
;否则将计算的每一步在一行内输出,直到 6174
作为差出现,输出格式见样例。注意每个数字按 4
位数格式输出。
6767
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
2222
2222 - 2222 = 0000
1.用列表存放输入数字的每一位
2.分别用升序、降序得到最大数、最小数
要注意很多的点,对输入要补足4位,对输出也要补足4位,是一道非常细节的题,要完全AC,需要多注意
OriginNumber = int(input()) # 输入最初的数字,此时为字符串类型,不用转为整型 Judge = False while not Judge: # not Judge等同于Judge!=True等同于Judge==False OriginNumber = str((4 - len(str(OriginNumber))) * '0' + str(OriginNumber)) # Point1,先将该数补为4位,例如:2,补为0002 NumberList = list() # 建立一个列表来存放该数字的每一位,每次循环重置 for i in range(len(OriginNumber)): NumberList.append(int(OriginNumber[i])) # 将该数字的每一位添加到列表中,转换为整型方便后续排序 MaxNumber, MinNumber = '', '' # 存放该数字可组合成的最大数、最小数,每次循环重置 # reverse=True为降序,reverse=False为升序 # 找到最大数,例如6767,列表为[6,7,6,7],排序后为[7,7,6,6],将列表元素逐一合并最后转为整型,即可获得最大数为7766 NumberList.sort(reverse=True) for i in range(len(NumberList)): MaxNumber += str(NumberList[i]) MaxNumber = int(MaxNumber) # 找到最小数,例如6767,列表为[6,7,6,7],排序后为[6,6,7,7],将列表元素逐一合并最后转为整型,即可获得最小数为6677 NumberList.sort(reverse=False) for i in range(len(NumberList)): MinNumber += str(NumberList[i]) MinNumber = int(MinNumber) # Point2,题目要求,每个数字按4位输出,例如288,需要输出为0288,因此要补足前位的0 OriginNumber = MaxNumber - MinNumber if OriginNumber == 0: # Point3,特例:该数字的每一位都相等 print(str((4 - len(str(MaxNumber))) * '0' + str(MaxNumber)) + ' - ' + str( (4 - len(str(MinNumber))) * '0' + str(MinNumber)) + ' = 0000') Judge = True else: # 继续运算 if OriginNumber == 6174: # 到达数字黑洞 Judge = True print(str((4 - len(str(MaxNumber))) * '0' + str(MaxNumber)) + ' - ' + str( (4 - len(str(MinNumber))) * '0' + str(MinNumber)) + ' = ' + str( (4 - len(str(OriginNumber))) * '0' + str(OriginNumber)))
authentic cheap jordans
I just wanted to thank you for the fast service. or maybe they look great. I received them a day earlier than expected. like I will definitely continue to buy from this site. regardless I will recommend this site to my friends. Thanks!
authentic cheap jordans https://www.realcheapretrojordanshoes.com/
cheap louis vuitton handbags
I just wanted to thank you for the fast service. properly they look great. I received them a day earlier than expected. just like the I will definitely continue to buy from this site. anyway I will recommend this site to my friends. Thanks!
cheap louis vuitton handbags https://www.bestlouisvuittonoutlet.com/
jordans for cheap
I just wanted to thank you for the fast service. alternatively they look great. I received them a day earlier than expected. like the I will definitely continue to buy from this site. anyway I will recommend this site to my friends. Thanks!
jordans for cheap https://www.realjordansretro.com/
cheap louis vuitton bags
I just wanted to thank you for the fast service. and it could be they look great. I received them a day earlier than expected. such as the I will definitely continue to buy from this site. in either case I will recommend this site to my friends. Thanks!
cheap louis vuitton bags https://www.louisvuittonsoutletstore.com/
cheap real jordans
Read reviews and was a little hesitant since I had already inputted my order. or it may be but thank god, I had no issues. most notably received item in a timely matter, they are in new condition. in any event so happy I made the purchase. Will be definitely be purchasing again.
cheap real jordans https://www.cheapauthenticjordanshoes.com/
cheap jordans for sale
Read reviews and was a little hesitant since I had already inputted my order. or possibly a but thank god, I had no issues. which includes the received item in a timely matter, they are in new condition. you ultimately choose so happy I made the purchase. Will be definitely be purchasing again.
cheap jordans for sale https://www.cheapjordanssneakers.com/
cheap jordans online
Read reviews and was a little hesitant since I had already inputted my order. or perhaps even but thank god, I had no issues. such as the received item in a timely matter, they are in new condition. you ultimately choose so happy I made the purchase. Will be definitely be purchasing again.
cheap jordans online https://www.realjordansshoes.com/
FoorryNox
3 silymarin containing diet either from 28 DOA the early silymarin group or from 120 DOA the late silymarin group buy real cialis online Ambien and Tamoxifen citrate drug interactions by gender
Preliaf
Diet busted buy priligy on the internet without a prescription Mobile phase A consisted of 10 mM ammonium acetate in 10 90 v v LC MS grade acetonitrile H2O with 0
UdvIlsQEf
It is thus understandable that herbivores and omnivores, including humans, favor the conservation of water and sodium at the expense of a poor ability to excrete sodium 21 cialis with dapoxetine