当前位置:网站首页>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;
}

边栏推荐
猜你喜欢

232. Implement queue with stack

为什么需要自动化测试?软件测试师带你测评不同软件测试工具

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

Bash shell学习笔记(二)

SCADA and three industrial control systems PLC, DCS and FCS

Wireshark basic tutorial Ethernet frame analysis.

Bash shell learning notes (I)

Visual conversion of nmap vulnerability scanning results

Implementing queues with two stacks

Bash shell learning notes (V)
随机推荐
Constructors, method overloads, object arrays, and static
0x00007ffd977c04a8 (qt5sqld.dll) (in a.exe): 0xc0000005: an access violation occurred when reading position 0x0000000000000010
35. Search the insertion position
菜鸟看源码之ArrayList
Wechat official account development obtains openid times error 40029 invalid code solution
Bash shell学习笔记(六)
Sword finger offer (twenty): stack containing min function
pytest 用例执行顺序
Bash shell learning notes (VII)
Build ARM embedded development environment
Tutorial of putty
菜鸟看源码之HashTable
Bash shell learning notes (6)
349. 两个数组的交集
Wireshark basic tutorial Ethernet frame analysis.
pytest pytest.ini配置 用例分组 用例跳过
10 let operator= return a reference to *this
WIRESHARK基础教程以太帧的分析。
Kali view IP address
Pengge C language 20210811 program structure operation