跳到主要內容

ACM409

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int prime(int p){
int i;
for(i=2;i<=sqrt(p);i++){
if(p%i==0)return 0;
}
return 1;
}
int main(int argc, char *argv[])
{
int i,j,stack[2000],n,c,k;
while(scanf("%d%d",&n,&c)!=EOF){

for(i=1,j=0;i<=n;i++){
if(prime(i))stack[j++]=i;
}
printf("%d %d:",n,c);
if(2*c>=j ||2*c-1>=j)for(i=0;i<j;i++)printf(" %d",stack[i]);
else if(j%2==0){
for(i=j/2-c;i<=j/2+c-1;i++){
printf(" %d",stack[i]);
}
}
else if(j%2==1){
for(i=j/2-c+1;i<=j/2+c-1;i++){
printf(" %d",stack[i]);
}
}
printf("\n\n");
}

return 0;
}

留言