Friday, February 18, 2011

10931 - Parity


#include<stdio.h>

int main()
{
    int n,i,j,k,ones;
    char bin[30];
    //freopen("in.txt","r",stdin);
    while(scanf("%d",&n)&&n)
    {
        ones=k=0;
        while(n)
        {
            if(n&1)
                ones++;
            bin[k++]=(n&1)+48;
            n=n>>1;
        }
        printf("The parity of ");
        for(j=k-1;j>=0;j--)
            printf("%c",bin[j]);
        printf(" is %d (mod 2).\n",ones);
    }
    return 0;
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.