B2140 二进制分类 题解
题目分析
这道题要求我们把
代码中用到的一些进制处理:
将
x>>n
将
x&y
(按位且的规则:将
那么,有了以上的铺垫,我们就能很轻松的提取了。我们这需要每次把数和
代码
#include <iostream>
using namespace std;
int A, B, cnt0, cnt1;
int main() {
int n, t, temp;
cin >> n;
for (int i = 1; i <= n; i++) {
cnt0 = 0, cnt1 = 0;
t = i;
while (t) {
temp = t & 1;
t = t >> 1;
if (temp) cnt1++;
else cnt0++;
}
cnt1 > cnt0 ? A++ : B++;
}
cout << A << ' ' << B;
}