题解 | 查找输入整数二进制中1的个数

查找输入整数二进制中1的个数

https://www.nowcoder.com/practice/1b46eb4cf3fa49b9965ac3c2c1caf5ad

#include <iostream>
#include <bitset>
using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    int count1 = 0, count2 = 0;
    bitset<32> b1(n);
    bitset<32> b2(m);
    string str1 = b1.to_string();
    string str2 = b2.to_string();
    for(int i = 0; i < 32; ++i) {
        if(str1[i] == '1') ++count1;
        if(str2[i] == '1') ++count2;
    }
    cout << count1 << endl;
    cout << count2 << endl;
    return 0;
}

bitset真好用

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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