题目:https://pintia.cn/problem-sets/994805260223102976/problems/994805309963354112
测试点4参考:https://blog.csdn.net/xyqqwer/article/details/89042444

这题有几个注意点

  1. 主程序中不要漏了get_prime
  2. 最后一个数字之后没有空格
  3. j < maxn 不能加等号,不然会显示数组越界
  4. 第10000个素数大约在105000 内
  5. 建立数组到max就够了

代码

  1. #include<vector>
  2. #include<algorithm>
  3. #include<iostream>
  4. #include<cstdio>
  5. using namespace std;
  6. const int maxn = 1000000;
  7. vector<bool> isprime(maxn, true);
  8. vector<int> prime;
  9. void get_prime(){
  10. for(int i = 2; i <= maxn; i++){
  11. if(isprime[i] == true){
  12. prime.push_back(i);
  13. for(int j = 2 * i; j < maxn; j += i){
  14. isprime[j] = false;
  15. }
  16. }
  17. }
  18. }
  19. int main(){
  20. int min, max;
  21. int linecount = 0;
  22. get_prime();
  23. scanf("%d %d",&min, &max);
  24. for(int i = min - 1; i < max; i++){
  25. cout<<prime[i];
  26. if(i == max-1) break;
  27. if(linecount!=9){
  28. printf(" ");
  29. linecount++;
  30. }
  31. else{
  32. printf("\n");
  33. linecount = 0;
  34. }
  35. }
  36. }