Tuesday 17 May 2011

406-prime cut

#include<math.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
        int j,k,n,c,pStor[200]={0},mid,exceed,pCount;
        //freopen("in.txt","r",stdin);
        while(scanf("%d%d",&n,&c)==2)
        {
        pStor[0]=1,pCount=1;
        printf("%d %d:",n,c);
        for(j=2;j<=n;j++)//a
        {
            for(k=2;k<=j;k++)
            {
                if(j%k==0)
                    break;
            }
            if(j==k)
            {
                pStor[pCount++]=j;
            }
        }//a
        if(pCount%2==0 && n!=0)
        {
            mid=(pCount/2)-c;
            exceed=c*2;
            if(exceed>pCount)
            {
                exceed=pCount;
                mid=0;
            }
            for(j=0;j<exceed;j++)
                printf(" %d",pStor[mid++]);
        }
        else if(pCount%2!=0 && n!=0)
        {
            mid=(pCount/2)-c+1;
            exceed=c*2-1;
            if(exceed>pCount)
            {
                exceed=pCount;
                mid=0;
            }
            for(j=0;j<exceed;j++)
                printf(" %d",pStor[mid++]);
        }
        printf("\n\n");
        }
    return 0;
}

No comments:

Post a Comment