博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
所有整数对之间的位差异数之和(Sum of bit differences among all pairs)
阅读量:4098 次
发布时间:2019-05-25

本文共 1242 字,大约阅读时间需要 4 分钟。

原文地址:

已知一个有n个整数的数组,找出数组中任意两个元素组成的整数的位差异数的和。

例子:

输入: arr[] = {1, 2} 输出: 4 数组中所有的整数对:(1, 1), (1, 2) (2, 1), (2, 2) 位差异数之和 = 0 + 2 + 2 + 0 = 4 输入: arr[] = {1, 3, 5} 输出: 8 数组中所有的整数对:(1, 1), (1, 3), (1, 5) (3, 1), (3, 3) (3, 5), (5, 1), (5, 3), (5, 5) 位差异数之和 = 0 + 1 + 1 + 1 + 0 + 2 + 1 + 2 + 0 = 8

一个简单的想法就是跑上两个循环逐个考虑所有的整数对,对于每一个整数对,计算位的差异。最后返回差异数的总和。这种方法的时间复杂度是O(n^2)。

有一个有效的方法可以在O(n)的时间内解决这个问题,它的方法就是将所有的数字都用32位(或者是其他固定位数)表示。这个想法是计数单独位的差异。我们可以遍历0到31并计数第i位的置位。设这个计数是“count”。这里将有“n - count”个数字的第i位没有置位,所以第i位的差异数是“count * (n - count) * 2”

下面是基于上述想法的C++实现:

// C++ program to compute sum of pairwise bit differences#include 
using namespace std;int sumBitDifferences(int arr[], int n){ int ans = 0; // Initialize result // traverse over all bits for (int i = 0; i < 32; i++) { // count number of elements with i'th bit set int count = 0; for (int j = 0; j < n; j++) if ( (arr[j] & (1 << i)) ) count++; // Add "count * (n - count) * 2" to the answer ans += (count * (n - count) * 2); } return ans;}// Driver prorgramint main(){ int arr[] = {
1, 3, 5}; int n = sizeof arr / sizeof arr[0]; cout << sumBitDifferences(arr, n) << endl; return 0;}

输出:

8

转载地址:http://ggqii.baihongyu.com/

你可能感兴趣的文章
Ubuntu部署
查看>>
四旋翼无人机的动力学模型
查看>>
PX4编译环境配置总结.....
查看>>
带参数的回调函数
查看>>
四元数的复数式和三角式的转换关系推导
查看>>
数据类型
查看>>
植保无人机PID调参经验
查看>>
AutoQuad数传问题
查看>>
ESP8266 WIFI数传 Pixhaw折腾笔记
查看>>
1.RT-Thread目录框架和启动过程分析
查看>>
3.RT-Thread线程的创建与删除,动态线程、静态进程
查看>>
7.RTT信号量的使用
查看>>
8.互斥量的使用--mutex
查看>>
9.邮箱的使用
查看>>
10.消息队列的使用
查看>>
2.RT-Thread中的跑马灯
查看>>
4.空闲任务与钩子函数
查看>>
5.中断和临界区的保护
查看>>
6.堆(动态内存 heap)的初始化和使用
查看>>
11.RT-Thread中的事件机制-event
查看>>