Love丶FFC's Blog

PAT乙级:1019 数字黑洞

2020-02-10 20:59:56
阅读:639   •   评论:10
标签:,

给定任一个各位数字不完全相同的 4 位正整数,如果我们先把 4 个数字按非递增排序,再按非递减排序,然后用第 1 个数字减第 2 个数字,将得到一个新的数字。一直重复这样做,我们很快会停在有“数字黑洞”之称的 6174,这个神奇的数字也叫 Kaprekar 常数。

例如,我们从6767开始,将得到

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

现给定任意 4 位正整数,请编写程序演示到达黑洞的过程。

输入格式:

输入给出一个 (0,10​4​​) 区间内的正整数 N

输出格式:

如果 N 的 4 位数字全相等,则在一行内输出 N - N = 0000;否则将计算的每一步在一行内输出,直到 6174 作为差出现,输出格式见样例。注意每个数字按 4 位数格式输出。

输入样例 1:

6767

输出样例 1:

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

输入样例 2:

2222

输出样例 2:

2222 - 2222 = 0000

编程语言:Python

解题思想:

1.用列表存放输入数字的每一位

2.分别用升序、降序得到最大数、最小数

要注意很多的点,对输入要补足4位,对输出也要补足4位,是一道非常细节的题,要完全AC,需要多注意

时间复杂度:O(logN)

代码如下:

  1. OriginNumber = int(input()) # 输入最初的数字,此时为字符串类型,不用转为整型
  2. Judge = False
  3.  
  4. while not Judge: # not Judge等同于Judge!=True等同于Judge==False
  5. OriginNumber = str((4 - len(str(OriginNumber))) * '0' + str(OriginNumber)) # Point1,先将该数补为4位,例如:2,补为0002
  6. NumberList = list() # 建立一个列表来存放该数字的每一位,每次循环重置
  7. for i in range(len(OriginNumber)):
  8. NumberList.append(int(OriginNumber[i])) # 将该数字的每一位添加到列表中,转换为整型方便后续排序
  9.  
  10. MaxNumber, MinNumber = '', '' # 存放该数字可组合成的最大数、最小数,每次循环重置
  11. # reverse=True为降序,reverse=False为升序
  12. # 找到最大数,例如6767,列表为[6,7,6,7],排序后为[7,7,6,6],将列表元素逐一合并最后转为整型,即可获得最大数为7766
  13. NumberList.sort(reverse=True)
  14. for i in range(len(NumberList)):
  15. MaxNumber += str(NumberList[i])
  16. MaxNumber = int(MaxNumber)
  17. # 找到最小数,例如6767,列表为[6,7,6,7],排序后为[6,6,7,7],将列表元素逐一合并最后转为整型,即可获得最小数为6677
  18. NumberList.sort(reverse=False)
  19. for i in range(len(NumberList)):
  20. MinNumber += str(NumberList[i])
  21. MinNumber = int(MinNumber)
  22.  
  23. # Point2,题目要求,每个数字按4位输出,例如288,需要输出为0288,因此要补足前位的0
  24. OriginNumber = MaxNumber - MinNumber
  25. if OriginNumber == 0: # Point3,特例:该数字的每一位都相等
  26. print(str((4 - len(str(MaxNumber))) * '0' + str(MaxNumber)) + ' - ' + str(
  27. (4 - len(str(MinNumber))) * '0' + str(MinNumber)) + ' = 0000')
  28. Judge = True
  29. else: # 继续运算
  30. if OriginNumber == 6174: # 到达数字黑洞
  31. Judge = True
  32. print(str((4 - len(str(MaxNumber))) * '0' + str(MaxNumber)) + ' - ' + str(
  33. (4 - len(str(MinNumber))) * '0' + str(MinNumber)) + ' = ' + str(
  34. (4 - len(str(OriginNumber))) * '0' + str(OriginNumber)))
  35.  

评论板

共有 10 条评论

  1. 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/

  2. 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/

  3. 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/

  4. 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/

  5. 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/

  6. 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/

  7. 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/

  8. 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

  9. 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

  10. 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

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名:http://www.loveffc:8080,点击跳转,完全移植后将去除端口号。

Copyright © 2018 - 2021 FFC的小站 - 滇 ICP 备 18010780 号 - 1

- Powered by WordPress & AliYun · Theme by FFC -

- Environment by Windows & XAMPP · Designed by WebStorm & VSCode -

已运行:

访问量:491171