Love丶FFC's Blog

PAT乙级:1012 数字分类

2020-02-03 16:29:43
阅读:630   •   评论:9
标签:,

给定一系列正整数,请按要求对数字进行分类,并输出以下 5 个数字:

A​1​​ = 能被 5 整除的数字中所有偶数的和;

A​2​​ = 将被 5 除后余 1 的数字按给出顺序进行交错求和,即计算 n​1​​−n​2​​+n​3​​−n​4​​⋯;

A​3​​ = 被 5 除后余 2 的数字的个数;

A​4​​ = 被 5 除后余 3 的数字的平均数,精确到小数点后 1 位;

A​5​​ = 被 5 除后余 4 的数字中最大数字。

输入格式:

每个输入包含 1 个测试用例。每个测试用例先给出一个不超过 1000 的正整数 N,随后给出 N 个不超过 1000 的待分类的正整数。数字间以空格分隔。

输出格式:

对给定的 N 个正整数,按题目要求计算 A​1​​~A​5​​ 并在一行中顺序输出。数字间以空格分隔,但行末不得有多余空格。

若其中某一类数字不存在,则在相应位置输出 N

输入样例 1:

13 1 2 3 4 5 6 7 8 9 10 20 16 18

输出样例 1:

30 11 2 9.7 9

输入样例 2:

8 1 2 4 5 6 7 9 16

输出样例 2:

N 11 2 N 9

编程语言:Python

解题思想:

1.逐个判断,添加到对应列表

2.对列表长度进行判断,进行后续操作

时间复杂度:O(N)

代码如下:

  1. Number = list(map(int, input().split()))
  2.  
  3. A1 = list()
  4. A2 = list()
  5. A3 = list()
  6. A4 = list()
  7. A5 = list()
  8. for content in Number[1:]:
  9. value = content % 5
  10. if value == 0 and content % 2 == 0:
  11. A1.append(content)
  12. elif value == 1:
  13. A2.append(content)
  14. elif value == 2:
  15. A3.append(content)
  16. elif value == 3:
  17. A4.append(content)
  18. elif value == 4:
  19. A5.append(content)
  20.  
  21. if len(A1) != 0:
  22. A1 = sum(A1)
  23. else:
  24. A1 = 'N'
  25.  
  26. if len(A2) != 0:
  27. StaggeredSum = 0
  28. for i in range(len(A2)):
  29. StaggeredSum += ((-1) ** i) * A2[i]
  30. A2 = StaggeredSum
  31. else:
  32. A2 = 'N'
  33.  
  34. if len(A3) != 0:
  35. A3 = len(A3)
  36. else:
  37. A3 = 'N'
  38.  
  39. if len(A4) != 0:
  40. A4 = round(sum(A4) / len(A4), 1)
  41. else:
  42. A4 = 'N'
  43.  
  44. if len(A5) != 0:
  45. A5 = max(A5)
  46. else:
  47. A5 = 'N'
  48.  
  49. print(A1, A2, A3, A4, A5)

评论板

共有 9 条评论

  1. louis vuitton outlet

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

  2. cheap jordans for sale

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

  3. cheap jordan shoes

    I just wanted to thank you for the fast service. possibly they look great. I received them a day earlier than expected. which includes I will definitely continue to buy from this site. no matter what I will recommend this site to my friends. Thanks!
    cheap jordan shoes https://www.realcheapretrojordanshoes.com/

  4. cheap jordans

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

  5. FoorryNox

    roman viagra review The rate of mucus plug was 82 41 50 and 6

  6. cheap jordans online

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

  7. cheap jordans

    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 received item in a timely matter, they are in new condition. either way so happy I made the purchase. Will be definitely be purchasing again.
    cheap jordans https://www.realcheapjordan.com/

  8. cheap louis vuitton

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

  9. Preliaf

    levitra generico andorra To maximize returns, add a few splurges in the master bath a linen closet, plus a separate shower stall and tub, which 82 of buyers rated as essential or desirable, according to the National Association of Homebuilders

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

已运行:

访问量:498589