题解 | #牛牛的10类人#

牛牛的10类人

https://www.nowcoder.com/practice/232b7fc32fac4636819e31a7d7c960a3

#include <iostream>
using namespace std;

string decimalToBinary(int decimal_num) {
    string binary_str = "";

    if (decimal_num == 0) {
        return "0";
    }

    while (decimal_num > 0) {
        binary_str = to_string(decimal_num % 2) + binary_str;
        decimal_num /= 2;
    }

    return binary_str;
}

int main() {
    int n;
    cin >> n ;
    int num;
    for (int i = 0; i < n; ++i) {
        cin >> num;
        string s=decimalToBinary(num);
        int count0 = 0;
        int count1 = 0;
        for (int i = 0; i < s.size(); ++i) {
            if (s[i] == '0') {
                count0++;
            } else {
                count1++;
            }
        }
        if (count0 % 2 == 0 && count1 % 2 == 0) {
            cout << "10 " ;
        } else if (count0 % 2 == 0 && count1 % 2 != 0) {
            cout << "0 ";
        } else if (count0 % 2 != 0 && count1 % 2 == 0) {
            cout << "1 ";
        } else {
            cout << "100 ";
        }


    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务