题目描述:
对一个数据a进行分类,
分类方法是,此数据a(4个字节大小)的4个字节相加对一个给定值b取模,
如果得到的结果小于一个给定的值c则数据a为有效类型,其类型为取模的值。
如果得到的结果大于或者等于c则数据a为无效类型。
比如一个数据a=0x01010101,b=3
按照分类方法计算:(0x01+0x01+0x01+0x01)%3=1
所以如果c等于2,则此a就是有效类型,其类型为1
如果c等于1,则此a是无效类型
又比如一个数据a=0x01010103,b=3
按分类方法计算:(0x01+0x01+0x01+0x03)%3=0
所以如果c=2则此a就是有效类型 其类型为0
如果c等于0 则此a是无效类型
输入12个数据,
第一个数据为c,第二个数据为b,
剩余10个数据为需要分类的数据
请找到有效类型中包含数据最多的类型,
并输出该类型含有多少个数据
输入描述
输入12个数据用空格分割,
第一个数据为c,第二个数据为b,
剩余10个数据为需要分类的数据。
输出描述
请找到有效类型中包含数据最多的类型,
并输出该类型含有多少个数据。
示例一
输入
3 4 256 257 258 259 260 261 262 263 264 265
输出
3
说明
这10个数据4个字节相加后的结果分别是
1 2 3 4 5 6 7 8 9 10
故对4取模的结果为
1 2 3 0 1 2 3 0 1 2
c是3所以012都是有效类型
类型为1和2的有3个数据
类型为0和3的只有两个
char GetBit(int val, char *bytes)
{
bytes[0] = (char)((0xff000000 & val) >> 24);
bytes[1] = (char)((0xff0000 & val) >> 16);
bytes[2] = (char)((0xff00 & val) >> 8);
bytes[3] = (char)((0xff & val) );
char num = bytes[0] + bytes[1] + bytes[2] + bytes[3];
return num;
}
using namespace std;
int main()
{
vector<int>arr;
//vector<int>res;
vector<int>res(10,0);
int num = 12, val = 0,c=0,b=0;
for (int i = 0; i < num; i++) {
if (i == 0) {
cin >> c;
continue;
}
else if (i == 1)
{
cin >> b;
continue;
}
cin >> val;
arr.push_back(val);
}
char bytes[4] = { 0,0,0,0 };
for (int i = 0; i < arr.size(); i++)
{
int num = GetBit(arr[i],bytes);
memset(bytes, 0, 4);
int x = num % b;
if (x < c) {
res[x] +=1;
}
}
sort(res.begin(), res.end());
reverse(res.begin(), res.end());
cout << res[0] << endl;
return 0;
}
#endif
二进制转整数,转字符串测试用例
#include<iostream>
#include<stack>
#include<vector>
#include<algorithm>
#include<string>
#include<bitset>
#include<list>
using namespace std;
*****************二进制转整数,转字符串************************************
int main()
{
bitset<8>bit1;
bit1.set(2);
cout << bit1 << endl;
string str = bit1.to_string();
int s1 = stoi(str);
cout << s1 << endl;
bitset<6>bit2;
bit2.set(2);
cout << bit2 << endl;
int x=bit2.to_ulong();
cout << x << endl;
}
*******************************整数转二进制******************************
递归输出二进制函数
void BinaryRecursion(int n)
{
int a;
a = n % 2;
n = n>>1;
if (n == 0) return;
else BinaryRecursion(n);
cout << a;
}
使用容器转换二进制
void BinaryVector(int n)
{
int temp;
temp = n;
list<int> L;
while (temp != 0)
{
L.push_front(temp % 2);
temp = temp>>1;
}
for (list<int>::iterator iter = L.begin(); iter != L.end(); iter++)
cout << * iter;
cout<<endl;
}
一般方法,32位,逐步与1做与运算。
void Binarycout(int n)
{
for (int i = 31; i>= 0; i--)
{
cout<<((n>>i) & 1);
}
cout<<endl;
}
//使用bitset转换二进制
void BinaryBitset(int n)
{
cout<<bitset<sizeof(int) * 8>(n)<<endl;
}
int main()
{
int a = 1045, b = 2;
int c;
c = a + b;
************以8为例*************************
int x = 8;
for (int i = 31; i >= 0; i--)
{
cout << ((x >> i) & 1);
}
**********以8为例子:*********
**********1000 右移i次(4-1次)->(0001)&1(0001)=1.********
cout<<"BinaryRecursion(c):";
BinaryRecursion(c);
cout << "BinaryVector(c):";
BinaryVector(c);
cout << "Binarycout(c):";
Binarycout(c);
cout << "BinaryBitset(c):";
BinaryBitset(c);
cout << "BinaryChar(c):";
return 0;
}
从整数中取二进制数
int x = 256;
char cd=(0xff000000 & x) >> 24;
cout << bitset<sizeof(int)>(cd) << endl;//0000
char c = (char)(0xff0000 & x) >> 16;
cout << bitset<sizeof(int)>(val) << endl;//0000
char ce = (0xff00 & x) >> 8;oxff00是取低位开始的十六位(一个零对应二进制数0000,两个零就是0000 0000,取后在右移八位就得到x中的0001)
cout << bitset<sizeof(int)>(ce) << endl;//0001
char cq = (0xff & x) ;oxff就是取x最后的八位
cout << bitset<sizeof(int)>(cq) << endl;//0000