当前位置:网站首页>Classified by the number of 1 in binary number
Classified by the number of 1 in binary number
2022-07-26 10:54:00 【Forest_ one thousand and ten】
Title Description : Small A Just learned binary , He was very excited . To make sure he did master binary , You gave him such a question : Given N Nonnegative integers , Will this N Numbers are given in binary 1 Number classification of , Under the binary 1 Numbers with the same number of belong to the same category . Find out how many kinds of numbers there are in the end ?
requirement
Input example :
1
5
8 2 5 7 3
Output example :
3
Code implementation
#include<iostream>
#include<vector>
#include<set>
using namespace std;
// In binary Computing 1 The number of
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<<" Please enter the number of samples :";
cin >>n;
cout<<endl;
for(int i=0;i<n;i++)
{
vector<int> v;
int num;
cout<<" The first "<<i+1<<" Number of group samples :";
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<<" The first "<<i+1<<" There are :";
int m=BinCounter(vec[i]);
cout<<m<<" class "<<endl;
}
system("pause");
return 0;
}

边栏推荐
猜你喜欢

Software Testing Overview: the background, essence and process of software testing

pytest pytest.ini配置 用例分组 用例跳过

使用Selenium抓取zabbix性能监控图

0x00007FFD977C04A8 (Qt5Sqld.dll)处(位于 a.exe 中)引发的异常: 0xC0000005: 读取位置 0x0000000000000010 时发生访问冲突

The problem of formatting IAR sprintf floating point to 0.0 in UCOS assembly

How to assemble a registry?

@Notblank, @notnull, @notempty differences and uses

349. 两个数组的交集

pytest fixture装饰器

Bash shell learning notes (6)
随机推荐
list升序和降序
242.有效的字母异位词
MFC picture control
RT thread learning notes (VII) -- open the elmfat file system based on SPI flash (middle)
2021-08-14 Sanzi chess
按二进制数中1的个数分类
RT thread learning notes (VIII) -- start the elmfat file system based on SPI flash (Part 2)
How to assemble a registry?
Pengge C language 20210811 program structure operation
The problem of formatting IAR sprintf floating point to 0.0 in UCOS assembly
MultipartFil转为File
C#委托与匿名方法浅析
Halcon模板匹配之Shape
软件测试综述之软件测试的背景、实质、软件开发的过程
@Notblank, @notnull, @notempty differences and uses
Constructors, method overloads, object arrays, and static
pytest 执行规则_基本用法_常用插件_常用断言_常用参数
Stringing of macro parameters and connection of macro parameters in C language
Minesweeping Pro version 2021-08-19
Tutorial of putty