题解 | #二进制计数-研发#

二进制计数-研发

http://www.nowcoder.com/questionTerminal/d38546d3b0a5416995a84ee9c7128d2b

#include<iostream>
using namespace std;

int main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int T;
    cin >> T;
    while (T--) {
        int map[32] = { 0 }; // 1的个数有可能有0-31
        int n;
        cin >> n;
        for (int i = 0; i < n; i++)
        {
            int num;
            cin >> num;
            int count = 0;
            while (num) {
                if (num & 1) count++; // 探测最后一位是否为1,是则加一
                num >>= 1; // 右移一位
            }
            map[count]++;
        }
        int size = 0;
        for (auto& it : map) {
            if (it > 0) {
                size++;
            }
        }
        cout << size << endl;
    }
}

交流一下❤

全部评论

相关推荐

点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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