Input
Each input set will be on a line by itself and will consist of 2 numbers. The first number (1 <= N <= 1000) is the maximum number in the complete list of prime numbers between 1 and N. The second number (1 <= C <= N) defines the C*2 prime numbers to be printed
from the center of the list if the length of the list is even; or the (C*2)-1 numbers to be printed from the center of the list if the length of the list is odd.
Output
For each input set, you should print the number N beginning in column 1 followed by a space, then by the number C, then by a colon (:), and then by the center numbers from the list of prime numbers as defined above. If the size of the center list exceeds the
limits of the list of prime numbers between 1 and N, the list of prime numbers between 1 and N (inclusive) should be printed. Each number from the center of the list should be preceded by exactly one blank. Each line of output should be followed by a blank
line. Hence, your output should follow the exact format shown in the sample output.
Sample Input
21 2
18 2
18 18
100 7
Sample Output
21 2: 5 7 11
18 2: 3 5 7 11
18 18: 1 2 3 5 7 11 13 17
100 7: 13 17 19 23 29 31 37 41 43 47 53 59 61 67
--------------------------------------------------------------------------------------------------------------------------------------------------
#include<string.h>
#include<stdio.h>
#define maxn 1200
int a[maxn];
void prime()
{
int i,j;
memset(a,0,sizeof(a));
a[0]=1;a[1]=0;a[2]=0;
for(i=2;i*i<maxn;i++)
{
if(!a[i])
{
for(j=i*i;j<maxn;j+=i)
a[j]=1;
}
}
}
int main()
{
int b[1000];
int i,j,k;
int n,c;
int start,end;
prime();
while(scanf("%d%d",&n,&c)!=EOF)
{
memset(b,0,sizeof(b));
k=0;
for(i=1;i<=n;i++)
{
if(!a[i])
b[k++] = i;
}
if(c>=k)
{
printf("%d %d:",n,c);
for(i=0;i<k;i++)
printf(" %d",b[i]);
printf("\n\n");
}
else
{
if(k&1)
{
start = (k-1)/2-(2*c-1)/2;
end = start + 2*c - 1;
printf("%d %d:",n,c);
for(i=start;i<end;i++)
printf(" %d",b[i]);
printf("\n\n");
}
else
{
start = (k-2)/2 -(2*c-2)/2;
end = k/2 +(2*c-2)/2;
printf("%d %d:",n,c);
for(i=start;i<=end;i++)
printf(" %d",b[i]);
printf("\n\n");
}
}
}
return 0;
}