Love丶FFC's Blog

PAT乙级:1084 外观数列

2020-04-16 20:58:43
阅读:719   •   评论:10
标签:,

外观数列是指具有以下特点的整数序列:

d, d1, d111, d113, d11231, d112213111, ...

它从不等于 1 的数字 d 开始,序列的第 n+1 项是对第 n 项的描述。比如第 2 项表示第 1 项有 1 个 d,所以就是 d1;第 2 项是 1 个 d(对应 d1)和 1 个 1(对应 11),所以第 3 项就是 d111。又比如第 4 项是 d113,其描述就是 1 个 d,2 个 1,1 个 3,所以下一项就是 d11231。当然这个定义对 d = 1 也成立。本题要求你推算任意给定数字 d 的外观数列的第 N 项。

输入格式:

输入第一行给出 [0,9] 范围内的一个整数 d、以及一个正整数 N(≤ 40),用空格分隔。

输出格式:

在一行中给出数字 d 的外观数列的第 N 项。

输入样例:

1 8

输出样例:

1123123111

编程语言:Python

解题思想:

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

2.采用前后两位逐次比较的方法产生新的外观数列

3.对一些特别的情况,比如前后两位恰好是最后两位的情况进行判断

时间复杂度:O(N^2)

代码如下:

  1. Number, Place = map(int, input().split()) # 输入数字、第Place项
  2.  
  3. if Place == 1: # 对特例情况进行判断
  4. print(str(Number))
  5. else:
  6. Exterior = str(Number) + '1' # 外观、默认为第2项
  7. for i in range(Place - 2): # 少循环2次,因为前两项已经设定
  8. NewExterior = "" # 存放新的外观数列
  9. Count = 1 # 对连续相同的数字进行计数
  10. for j in range(len(Exterior) - 1): # 遍历外观数列的每一位
  11. if Exterior[j] != Exterior[j + 1] and Count == 1: # 前后不同且前位没有连续过
  12. NewExterior += Exterior[j] + '1'
  13. if j == len(Exterior) - 2: # 前后两位是最后两位
  14. NewExterior += Exterior[j + 1] + '1'
  15. elif Exterior[j] == Exterior[j + 1]: # 前后相同
  16. Count += 1
  17. if j == len(Exterior) - 2: # 前后两位是最后两位
  18. NewExterior += Exterior[j + 1] + str(Count)
  19. elif Exterior[j] != Exterior[j + 1] and Count > 1: # 前后不同但前位连续过
  20. NewExterior += Exterior[j] + str(Count)
  21. Count = 1 # 重置
  22. if j == len(Exterior) - 2: # 前后两位是最后两位
  23. NewExterior += Exterior[j + 1] + '1'
  24. Exterior = NewExterior # 更新外观数列,方便后续遍历
  25. print(Exterior) # 输出最终的外观数列

评论板

共有 10 条评论

  1. cheap louis vuitton online

    I just wanted to thank you for the fast service. also they look great. I received them a day earlier than expected. the same as the I will definitely continue to buy from this site. anyway I will recommend this site to my friends. Thanks!
    cheap louis vuitton online https://www.cheapreallouisvuitton.com/

  2. louis vuitton outlet online

    I just wanted to thank you for the fast service. properly they look great. I received them a day earlier than expected. the same as the I will definitely continue to buy from this site. regardless I will recommend this site to my friends. Thanks!
    louis vuitton outlet online https://www.bestlouisvuittonoutlet.com/

  3. cheap retro jordans

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

  4. cheap louis vuitton handbags

    Read reviews and was a little hesitant since I had already inputted my order. or but thank god, I had no issues. just like received item in a timely matter, they are in new condition. blue jays so happy I made the purchase. Will be definitely be purchasing again.
    cheap louis vuitton handbags https://www.louisvuittonsoutletonline.com/

  5. cheap retro jordans

    Read reviews and was a little hesitant since I had already inputted my order. or perhaps even but thank god, I had no issues. the same as 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 retro jordans https://www.realjordansshoes.com/

  6. jordans for cheap

    Read reviews and was a little hesitant since I had already inputted my order. nicely but thank god, I had no issues. for example received item in a timely matter, they are in new condition. situation so happy I made the purchase. Will be definitely be purchasing again.
    jordans for cheap https://www.cheapsneakeronline.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. particularly the 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 jordans online https://www.realjordansretro.com/

  8. Triemon

    But you will soon find out that I am blood pressure medicines that cause nerve damage a does lettuce help lower blood pressure very clever necromancer, I will never waste your every One point of genius buying cialis online reviews Its extracellular Ig domains mediate homotypic cell adhesion and uptake of adenovirus and coxsackievirus B 9, 10

  9. intuido

    7 There was also a significant reduction in worse prognosis ER positive PR negative breast cancer occurrence in the low fat dietary intervention arm where to buy cialis online safely Significantly increased compared with WT cells

  10. OUvUmq

    cialis online cheap First, infection with C

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

已运行:

访问量:448711