#include <stdio.h> int NumberOf1(int n); int main() { int a, b; scanf("%d %d",&a,&b); int temp=a^b; int cot=NumberOf1(temp); printf("%d",cot); return 0; } int NumberOf1(int n) { int cot = 0; while (n) { n = n & (n - 1); cot++; } return cot; }
利用按位异或的性质:位相同为0,相异为1;
将问题转变为统计一个整数二进制表示中1的个数。