题解 | #牛牛的10类人#
牛牛的10类人
https://www.nowcoder.com/practice/232b7fc32fac4636819e31a7d7c960a3
#include <iostream> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = 0; i < n; ++i) { int num_one = 0; int num_zero = 0; int temp = a[i]; do { if (temp & 1) num_one++; else num_zero++; }while (temp >>= 1, temp != 0); if (num_one % 2 == 0 && num_zero % 2 == 0) cout << 10 << " "; else if (num_one % 2 == 0) cout << 1 << " "; else if (num_zero % 2 == 0) cout << 0 << " "; else cout << 100 << " "; } } // 64 位输出请用 printf("%lld")
最关键的是这行代码:
do { if (temp & 1) num_one++; else num_zero++; }while (temp >>= 1, temp != 0);
C++题解 文章被收录于专栏
记录在牛客网用C++刷题的题解思路