1182 - Parity
Time Limit: 0.5 second(s) | Memory Limit: 32 MB |
Given an integer n, first we represent it in binary.Then we count the number of ones. We say n has odd parity if the numberof one's is odd. Otherwise we say n has even parity. 21 = (10101)2has odd parity since the number of one's is 3. 6 = (110)2has even parity.
Now you are given n, we have to say whether nhas even or odd parity.
Input
Input starts with an integer T (≤ 1000),denoting the number of test cases.
Each case contains an integer n (1 ≤ n < 231).
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;
int main(){
int i,j,k,m,n,T,ans;
scanf("%d",&T);
for(i=1;i<=T;i++){
scanf("%d",&n);
ans=0;
while(n!=0){
if(n%2==1)ans++;
n/=2;
}
if(ans%2==1)printf("Case %d: odd\n",i);
else printf("Case %d: even\n",i);
}
return 0;罗旅洲
}
Output
For each case, print the case number and 'odd' if nhas odd parity, otherwise print 'even'.
二进制下1的个数。odd:奇数,even偶数