2013年1月4日 星期五

[ZJ] a132. 10931 - Parity


內容 :
整數 n 的「同位元」定義為:其二進位表示法中每位元的和再除以 2 的餘數。例如:21 = 101012 的二進位有三個 1,因此它的同位元為 3 (mod 2),或 1
在此,你要計算一個整數 1 ≤ I ≤ 2147483647 的同位元。
輸入說明 :
輸入的每一行有一個整數 I,而 I = 0 表示輸入結束,該行無需處理。
輸出說明 :
對於輸入中的每個整 I,你要印一行 The parity of B is P (mod 2).,其中 B 是 I 的二進位表示法。
範例輸入 :

1210210
範例輸出 :
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).
提示 :
出處 :
UVa ACM 10931 (管理:snail)

/**********************************************************************************/
/*  Problem: a132 "10931 - Parity" from UVa ACM 10931                             */
/*  Language: C                                                                   */
/*  Result: AC (6ms, 276KB) on ZeroJudge                                          */
/*  Author: morris1028 at 2011-05-29 18:20:40                                     */
/**********************************************************************************/


#include<stdio.h>
int t, N;
int D(int N) {
    if(N) {
        D(N/2);
        printf("%d",N&1), t += N&1;
    }
}
main() {
    while(scanf("%d", &N) == 1 && N) {
        printf("The parity of ");
        t = 0, D(N);
        printf(" is %d (mod 2).\n", t);
    }
    return 0;
}

沒有留言:

張貼留言