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

二进制计数-研发

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;
    }
}

交流一下❤

全部评论

相关推荐

LXXXXd:有点杂,想搞自动化的话没必要把法律的经历写上去
点赞 评论 收藏
分享
09-25 00:00
已编辑
电子科技大学 Java
球球与墩墩:这不是前端常考的对象扁平化吗,面试官像是前端出来的 const flattern = (obj) => { const res = {}; const dfs = (curr, path) => { if(typeof curr === 'object' && curr !== null) { const isArray = Array.isArray(curr); for(let key in curr) { const newPath = path ? isArray ? `${path}[${key}]` : `${path}.${key}` : key; dfs(curr[key], newPath); } } else { res[path] = curr } } dfs(obj); return res; }
查看3道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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