当前位置:网站首页>按二进制数中1的个数分类
按二进制数中1的个数分类
2022-07-26 10:42:00 【Forest_1010】
题目描述:小A刚学了二进制,他十分激动。为了确定他的确掌握了二进制,你给他出了这样一道题目:给定N个非负整数,将这N个数字按照二进制下1的个数分类,二进制下1的个数相同的数字属于同一类。求最后一共有几类数字?
要求
输入例子:
1
5
8 2 5 7 3
输出例子:
3
代码实现
#include<iostream>
#include<vector>
#include<set>
using namespace std;
//计算二进制中1的个数
int BinCounter(vector<int> v)
{
set<int>res;
for(int i=0;i<v.size();i++)
{
int counter=0;
while(v[i]!=0)
{
v[i]=v[i]&(v[i]-1);
counter++;
}
res.insert(counter);
}
return res.size();
}
int main()
{
vector<vector<int>> vec;
int n;
cout<<"请输入样例个数:";
cin >>n;
cout<<endl;
for(int i=0;i<n;i++)
{
vector<int> v;
int num;
cout<<"第 "<<i+1<<" 组样例个数:";
cin>>num;
cout<<endl;
for(int j=0;j<num;j++)
{
int val;
cin>>val;
v.push_back(val);
}
vec.push_back(v);
cout<<endl;
}
for(int i=0;i<vec.size();i++)
{
cout<<"第"<<i+1<<"组样例中一共有:";
int m=BinCounter(vec[i]);
cout<<m<<" 类"<<endl;
}
system("pause");
return 0;
}

边栏推荐
- 文案秘籍七步曲至----文献团队协作管理
- Build ARM embedded development environment
- Summary of common skills in H5 development of mobile terminal
- Database functions
- STM32 Alibaba cloud mqtt esp8266 at command
- 剑指Offer(九):变态跳台阶
- [leetcode daily question 2021/8/30]528. Choose randomly by weight [medium]
- 剑指Offer(五):用两个栈实现队列
- [leetcode daily question 2021/4/29]403. Frogs cross the river
- C语言鹏哥20210812C语言函数
猜你喜欢
![[notes on machine learning] [building a cyclic neural network and its application] deeplearning ai course5 1st week programming(keras)](/img/02/f85da2a2f2524fb034b17ed8d06692.png)
[notes on machine learning] [building a cyclic neural network and its application] deeplearning ai course5 1st week programming(keras)

RT-Thread 学习笔记(五)---编辑、下载、调试程序
![Error[pe147]: declaration is incompatible with 'error problem](/img/4f/57145d78f4dc1fe84d2f271dd9d82f.png)
Error[pe147]: declaration is incompatible with 'error problem

第6期:大学生应该选择哪种主流编程语言

2021-08-12函数递归_和鹏哥学习C语言

20210807#1 C语言程序结构
![[leetcode daily question 2021/8/30]528. Choose randomly by weight [medium]](/img/13/c6cb176d7065035f60d55ad20ed1bf.png)
[leetcode daily question 2021/8/30]528. Choose randomly by weight [medium]
![[leetcode每日一题2021/2/18]【详解】995. K 连续位的最小翻转次数](/img/de/62fca587cde95110c2a967ca93eea5.png)
[leetcode每日一题2021/2/18]【详解】995. K 连续位的最小翻转次数
![[leetcode每日一题2021/8/30]528. 按权重随机选择【中等】](/img/13/c6cb176d7065035f60d55ad20ed1bf.png)
[leetcode每日一题2021/8/30]528. 按权重随机选择【中等】

Mlx90640 infrared thermal imager temperature sensor module development notes (VI) pseudo color coding of infrared images
随机推荐
Common classes (understand)
【小程序】onReachBottom 事件为什么不能触发 ?(一秒搞定)
【机器学习小记】【搭建循环神经网络及其应用】deeplearning.ai course5 1st week programming(keras)
常见的类(了解)
MD5 encryption
Sql Server 数据库之初学体验
Redis docker instance and data structure
Oracle cannot start tnslistener service cannot start
Flutter编译报错 version of NDK matched the requested version 21.0.6113669. Versions available locally: 2
异常的概念与处理
鹏哥C语言20210811程序结构作业
putty的使用教程
Oracle创建索引
px2rem-loader将px转化为rem,适配移动端vant-UI等框架
Issue 8: cloud native -- how should college students learn in the workplace
2021-08-12函数递归_和鹏哥学习C语言
第7期:内卷和躺平,你怎么选
Sql Server 数据库之完整性约束
在altium designer中禁用USBJATG
工厂模式详解