欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UVA10931 Parity【进制】

程序员文章站 2022-07-15 09:34:16
...

We define the parity of an integer n as the sum of the bits in binary representation computed modulo two. As an example, the number 21 = 101012 has three 1s in its binary representation so it has parity 3(mod2), or 1.

  In this problem you have to calculate the parity of an integer 1 ≤ I ≤ 2147483647.

Input

Each line of the input has an integer I and the end of the input is indicated by a line where I = 0 that should not be processed.

Output

For each integer I in the inputt you should print a line ‘The parity of B is P (mod 2).’, where B is the binary representation of I.

Sample Input

1

2

10

21

0

Sample Output

The parity of 1 is 1 (mod 2).

The parity of 10 is 1 (mod 2).

The parity of 1010 is 2 (mod 2).

The parity of 10101 is 3 (mod 2).


题链接UVA10931 Parity

问题简述:(略)

问题分析

  简单的10进制转2进制问题,再对2进制各位数字求和即可。

程序说明:(略)

题记:(略)

参考链接:(略)


AC的C++语言程序如下:

/* UVA10931 Parity */

#include <bits/stdc++.h>

using namespace std;

const int N = 64;
char bits[N + 1];

int main()
{
    int n;
    while(~scanf("%d", &n) && n) {
        int i = 0, sum = 0;
        while(n) {
            sum += n & 1;
            bits[i++] = (n & 1) + '0';
            n >>= 1;
        }

        printf("The parity of ");
        while(i)
            putchar(bits[--i]);
        printf(" is %d (mod 2).\n", sum);
    }

    return 0;
}