1013 数素数 (20 分)—PAT (Basic Level) Practice (中文)
程序员文章站
2024-03-19 08:24:52
...
1013 数素数 (20 分)
令 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
#include<stdio.h>
#include<math.h>
int scnprim(int n);
int main()
{
int i,m,n,count=0;
scanf("%d %d",&m,&n);
for(i=2;;i++)
{
if(scnprim(i))
{
count++;
if(m<=count&&n>=count)
{ if((count-m+1)%10==0)
printf("%d\n",i);
else if(count!=n)
printf("%d ",i);
else
printf("%d",i);
}
}
if(n<count)
{
break;
}
}
return 0;
}
int scnprim(int n)//筛选素数
{
int sqrtc,j;
sqrtc=(int)sqrt(n);
for(j=2;j<=sqrtc;j++)
{
if(n%j==0)
return 0;
}
return n;
}
推荐阅读
-
PAT (Basic Level) Practice (中文)1016 部分A+B (15分)
-
1013 数素数 (20 分)—PAT (Basic Level) Practice (中文)
-
PAT (Basic Level) Practice 1004 成绩排名 (20 分)
-
PAT (Basic Level) Practice (中文) 1016 部分A+B (15 分)
-
1006 换个格式输出整数 (15分) Java题解 PAT (Basic Level) Practice (中文)
-
PAT (Basic Level) Practice (中文)1003 我要通过! (20 分)
-
PAT (Basic Level) Practice (中文)1038 统计同成绩学生 (20 分)
-
PAT (Basic Level) Practice (中文)1006 换个格式输出整数 (15 分)
-
PAT (Basic Level) Practice (中文)--1038 统计同成绩学生 (20 分)
-
【PAT (Basic Level) Practice (中文)】1038 统计同成绩学生 (20 分)题解