题目描述
输入格式:
输入在一行中给出 M 和 N,其间以空格分隔。
输出格式:
输出从 PM 到 PN 的所有素数,每 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
提交截图
该说不说,这道题的格式错误我调试了大半天。。。。难度倒是不高,但是我用的方法比较笨,就是把10000个素数全都找出来了= = ,好像也可以用J做变量,是个好办法,不过也是看了别人的才学会。代码就改一个条件(j<=10000)。起码我知道了10000个素数是多少,也算是个收获把= = 。
代码
//
#include<stdio.h>
#include<math.h>
int isprime(int n){
int temp = sqrt(n);
for(int i=2;i<=temp;i++){
if(n%i ==0){
return 0;
}
}
return 1;
}
int main(){
int n,m,j = 1;
scanf("%d%d",&n,&m);
int a[10001] = {0};
a[j] = 2;
j++;
for(int i=3;i<104730;i++){
if(isprime(i) == 1){
a[j] = i;
j++;
}
}
int count = 0;
for(int i=n;i<=m;i++){
count++;
if(count == (m-n+1)){
printf("%d",a[i]);
}
else{
if(count % 10!=0){
printf("%d ",a[i]);
}
else{
printf("%d\n",a[i]);
}
}
}
printf("\n");
return 0;
}