数据分类对一个数据a进行分类,分类方法为:此数据a(四个字节大小)的四个字节相加对一个给定的值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个数据为需要分类的数据。输出描述:输出最多数据的有效类型有多少个数据。示例1输入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,所以0 1 2都是有效类型,类型为1和2的有3个数据,类型为0的只有2个数据,故输出3示例2输入1 4 256 257 258 259 260 261 262 263 264 265输出2说明10个数据4个字节相加后的结果分别为1 2 3 4 5 6 7 8 9 10,故对4取模的结果为1 2 3 0 1 2 3 0 1 2,c为1,所以只有0是有效类型,类型为0的有2个数据,故输出2*/const arr = [3, 4, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265];const c = parseInt(arr[0]);const b = parseInt(arr[1]);const dataArr = arr.slice(2).map((str) => parseInt(str, 16));const validTypes = [];for (const data of dataArr) {  const sum =    ((data >> 24) & 0xff) +    ((data >> 16) & 0xff) +    ((data >> 8) & 0xff) +    (data & 0xff);    console.log(sum)  const type = sum % b;  if (type < c) {    validTypes.push(type);  }}const typeCounts = {};for (const type of validTypes) {  if (!typeCounts.hasOwnProperty(type)) {    typeCounts[type] = 1;  } else {    typeCounts[type]++;  }}let maxType = null;let maxCount = 0;for (const type in typeCounts) {  if (typeCounts[type] > maxCount) {    maxCount = typeCounts[type];    maxType = type;  }}console.log(maxCount);
点赞 3
评论 1
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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