Love丶FFC's Blog

PAT乙级:1079 延迟的回文数

2020-04-11 13:03:15
阅读:1308   •   评论:13
标签:,

给定一个 k+1 位的正整数 N,写成 a​k​​⋯a​1​​a​0​​的形式,其中对所有 i 有 0≤a​i​​<10 且 a​k​​>0。N 被称为一个回文数,当且仅当对所有 i有 a​i​​=a​k−i​​。零也被定义为一个回文数。

非回文数也可以通过一系列操作变出回文数。首先将该数字逆转,再将逆转数与该数相加,如果和还不是一个回文数,就重复这个逆转再相加的操作,直到一个回文数出现。如果一个非回文数可以变出回文数,就称这个数为延迟的回文数。(定义翻译自 https://en.wikipedia.org/wiki/Palindromic_number )

给定任意一个正整数,本题要求你找到其变出的那个回文数。

输入格式:

输入在一行中给出一个不超过1000位的正整数。

输出格式:

对给定的整数,一行一行输出其变出回文数的过程。每行格式如下

A + B = C

其中 A 是原始的数字,B 是 A 的逆转数,C 是它们的和。A 从输入的整数开始。重复操作直到 C 在 10 步以内变成回文数,这时在一行中输出 C is a palindromic number.;或者如果 10 步都没能得到回文数,最后就在一行中输出 Not found in 10 iterations.

输入样例 1:

97152

输出样例 1:

97152 + 25179 = 122331
122331 + 133221 = 255552
255552 is a palindromic number.

输入样例 2:

196

输出样例 2:

196 + 691 = 887
887 + 788 = 1675
1675 + 5761 = 7436
7436 + 6347 = 13783
13783 + 38731 = 52514
52514 + 41525 = 94039
94039 + 93049 = 187088
187088 + 880781 = 1067869
1067869 + 9687601 = 10755470
10755470 + 07455701 = 18211171
Not found in 10 iterations.

编程语言:Python

解题思想:

1.首先对特例情况进行判断

2.利用字符串拼接得到B

3.判断是否回文可以使用两边往中间逐位比较的方法,如165561,依次进行1==1、6==6、5==5的比较

时间复杂度:O(NlogN)

代码如下:

  1. Number = input() # 输入原始数字
  2. Count = 0 # 操作的次数
  3. A = Number
  4.  
  5. Palindrome = True # 标记是否为回文数,默认为是
  6. for i in range(len(A)): # 特例:输入的数就是回文数
  7. if A[i] != A[len(A) - i - 1]:
  8. Palindrome = False
  9. break
  10.  
  11. if Palindrome:
  12. print(A + " is a palindromic number.")
  13. else:
  14. while True:
  15. B = "" # 逆转数
  16. for i in range(len(A) - 1, -1, -1):
  17. B += A[i]
  18. C = str(int(A) + int(B))
  19. Count += 1 # 完成一次操作
  20. print(A + " + " + B + " = " + C) # 输出运算结果
  21. Palindrome = True # 每次都将标记重置
  22. for i in range(len(C)): # 遍历C,从左边、右边同时逐位往中间判断
  23. if C[i] != C[len(C) - i - 1]: # 两字符不相同
  24. Palindrome = False
  25. break
  26. if Palindrome: # C是回文数
  27. print(C + " is a palindromic number.")
  28. break
  29. else: # C不是回文数,继续操作
  30. A = C
  31. if Count == 10: # 操作次数超过规定
  32. print("Not found in 10 iterations.")
  33. break

评论板

共有 13 条评论

  1. cheap louis vuitton handbags

    I just wanted to thank you for the fast service. maybe they look great. I received them a day earlier than expected. including the I will definitely continue to buy from this site. you ultimately choose I will recommend this site to my friends. Thanks!
    cheap louis vuitton handbags https://www.bestlouisvuittonoutlet.com/

  2. cheap jordans for sale

    I just wanted to thank you for the fast service. or else they look great. I received them a day earlier than expected. for example I will definitely continue to buy from this site. anyway I will recommend this site to my friends. Thanks!
    cheap jordans for sale https://www.cheaprealjordan.com/

  3. cheap jordans

    I just wanted to thank you for the fast service. or it may be they look great. I received them a day earlier than expected. such as the I will definitely continue to buy from this site. blue jays I will recommend this site to my friends. Thanks!
    cheap jordans https://www.realjordansretro.com/

  4. original louis vuittons outlet

    I just wanted to thank you for the fast service. aka they look great. I received them a day earlier than expected. for example I will definitely continue to buy from this site. anyway I will recommend this site to my friends. Thanks!
    original louis vuittons outlet https://www.cheapreallouisvuitton.com/

  5. cheap louis vuitton online

    I just wanted to thank you for the fast service. or maybe they look great. I received them a day earlier than expected. just like the I will definitely continue to buy from this site. in any event I will recommend this site to my friends. Thanks!
    cheap louis vuitton online https://www.louisvuittonsoutletstore.com/

  6. Gealpaste

    Helping Carlos or others is for the sake of It is only a means to achieve this goal, Now is not the time for internal how long how long to allow for new blood pressure medicine to allow for new blood pressure medicine fighting, Marshal Rocherio, Our betrayal puts us at a great disadvantage, so temporary apparent weakness is not a sign of cowardice cheap cialis online

  7. intuido

    Ask your doctor to tell you about the specific harm that may be done to the fetus cheapest propecia uk

  8. mYbBcH

    Serious Use Alternative 1 enoxaparin, zanubrutinib priligy over the counter usa Patients should be evaluated for future anxiety and potential PTSD symptoms when they are initiated on new medications

  9. Hgazmh

    buy fenofibrate 200mg pills order fenofibrate 160mg for sale buy generic fenofibrate for sale

  10. Wlzvuj

    generic cialis canada viagra 100mg sale purchase sildenafil

  11. Innqfx

    order ketotifen generic ketotifen 1 mg without prescription imipramine 75mg for sale

  12. Kudbyp

    buy minoxidil generic buy ed pills best price buy generic ed pills over the counter

  13. Ripycd

    precose 50mg brand purchase fulvicin without prescription order fulvicin 250 mg without prescription

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名: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 -

已运行:

访问量:510299