Love丶FFC's Blog

PAT乙级:1013 数素数

2020-02-04 10:07:15
阅读:724   •   评论:12
标签:,

令 P​i​​ 表示第 i 个素数。现任给两个正整数 M≤N≤10​4​​,请输出 P​M​​ 到 P​N​​ 的所有素数。

输入格式:

输入在一行中给出 M 和 N,其间以空格分隔。

输出格式:

输出从 P​M​​ 到 P​N​​  的所有素数,每 10 个数字占 1 行,其间以空格分隔,但行末不得有多余空格。

输入样例:

5 27

输出样例:

11 13 17 19 23 29 31 37 41 43
47 53 59 61 67 71 73 79 83 89
97 101 103

解题思想:

1.使用常规的素数判断方法进行判断,可以进一步优化为埃氏筛法

2.注意题目的边界,对各种输出的格式进行判断

时间复杂度:O(NlogN)

代码如下(Python):

  1. import math
  2.  
  3. Start, End = map(int, input().split())
  4. PrimeJudge = True # 标记该数是否为素数,默认为是
  5. PrimeCount = 0 # 记录当前素数的位置
  6. PrimeNumber = list() # 存储素数的列表
  7. PrintCount = 0 # 控制输出的计数
  8. for i in range(2, 200000):#遍历一个足够满足条件的边界
  9. PrimeJudge = True # 每次判断一个数就重置标记
  10. for j in range(2, int(math.sqrt(i))+1):
  11. if i % j == 0:
  12. PrimeJudge = False
  13. break
  14. if PrimeJudge == True:
  15. PrimeCount += 1
  16. if Start <= PrimeCount <= End:#该素数满足范围
  17. PrimeNumber.append(i)
  18. if PrimeCount > End:#素数个数已经超出范围
  19. break
  20. for i in range(len(PrimeNumber)):#按题目格式输出
  21. PrintCount += 1
  22. if i == len(PrimeNumber) - 1:
  23. print(PrimeNumber[len(PrimeNumber) - 1])
  24. elif PrintCount < 10:
  25. print(PrimeNumber[i], end=" ")
  26. else:
  27. print(PrimeNumber[i])
  28. PrintCount = 0
  29.  

使用Python,即使已经降到NlogN的时间复杂度,仍会超时 。

代码如下(C):

  1. #include<stdio.h>
  2. #include<math.h>
  3. int main(void)
  4. {
  5. int Start,End;
  6. scanf("%d %d",&Start,&End);
  7. int count = 1;//记录素数的位置,默认为1,2是第一个素数
  8. int printcount = 0;//标记输出的情况
  9. if(Start==1)//对特例进行判断,用不同的格式输出
  10. {
  11. if(End == 1)//只输出第一个素数
  12. {
  13. printf("2");
  14. }
  15. else
  16. {
  17. printf("2 ");
  18. printcount++;
  19. }
  20. }
  21. for(int i=3;i<200000;i++)//定义足够大的边界
  22. {
  23. int primejudge = 1;//标记该数是否为素数,默认为是
  24. for(int j=2;j<sqrt(i)+1;j++)
  25. {
  26. if(i%j==0)//该数能被1和自己以外的数整除
  27. {
  28. primejudge = 0;//改变标记
  29. break;
  30. }
  31. }
  32. if(primejudge == 1)//该数是素数
  33. {
  34. count++;
  35. if(count >= Start && count <= End)//该素数所在的位置符合输入的条件
  36. {
  37. if(printcount < 9)//输出不需要换行
  38. {
  39. if(count == End)//该素数是输入的边界
  40. {
  41. printf("%d",i);
  42. }
  43. else
  44. {
  45. printf("%d ",i);
  46. }
  47. printcount++;
  48. }
  49. else//输出可能需要换行
  50. {
  51. if(count == End)//该素数是输入的边界也是一行的最后一个输出
  52. {
  53. printf("%d",i);
  54. }
  55. else
  56. {
  57. printf("%d\n",i);
  58. }
  59. printcount = 0;
  60. }
  61. }
  62. if(count > End)//寻找的范围超过输入的边界
  63. {
  64. break;
  65. }
  66. }
  67. }
  68. return 0;
  69. }

评论板

共有 12 条评论

  1. fruinly

    However, bone health can become compromised by ageing and or the presence of underlying medical conditions how does azithromycin work In addition, on her 2nd, 3rd, and 4th fingernails bilaterally, clinicians noticed a single transverse white line, parallel to the lunula

  2. engarma

    Who knew that the national teacher was beaten and almost died after only three face to face encounters buy cialis non prescription

  3. cheap louis vuitton online

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

  4. cheap louis vuitton bags

    I just wanted to thank you for the fast service. actually they look great. I received them a day earlier than expected. enjoy 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 bags https://www.louisvuittonsoutletstore.com/

  5. cheap jordans online

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

  6. louis vuitton outlet

    I just wanted to thank you for the fast service. or alternatively they look great. I received them a day earlier than expected. exactly like the I will definitely continue to buy from this site. either way I will recommend this site to my friends. Thanks!
    louis vuitton outlet https://www.cheapreallouisvuitton.com/

  7. cheap louis vuitton outlet

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

  8. cheap louis vuitton handbags

    Read reviews and was a little hesitant since I had already inputted my order. while well as but thank god, I had no issues. such as 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 louis vuitton handbags https://www.louisvuittonsoutletonline.com/

  9. cheap jordans for sale

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

  10. Preliaf

    When we mentioned wide state variations, Schank said It s noticeable if you have traveled to other cities or states that every place has their own rules that they are really, really adamant about, and you have no idea what those rules are cialis without a doctor’s prescription Interestingly, the four patients with recurrent VTE who received only IVC filters had longer MTRs 6

  11. Preliaf

    Member, Ontario Medical Association prix levitra 10 mg

  12. Fsmppgw

    I know how you feel just wanting your body to do the work itself and I ve thought about natural, herbal alternatives cialis daily

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

已运行:

访问量:509772