子网掩码+ip地址_C ++程序使用位掩码查找唯一编号

子网掩码+ip地址

Problem statement: C++ Program to find unique number in an array of n numbers in which except one (unique number) rest all are present thrice.

问题陈述: C ++程序在n个数字的数组中查找唯一数字,其中除一个(唯一数字)外其余所有其余三次。

Constraints: n<10^5

约束: n <10 ^ 5

Example:

例:

    Input:
10
1  2  3  2  4  1  2  3  1  3
Output:
4

Solution: Except 4 rest all have multiplicity of 3 in array. For instance 4 are represented as 100 in binary .Starting from left most bit is the first index of count array.

解决方案:除了4个其余的所有数组都具有3的多重性。 例如,4用二进制表示为100.从最左边的位开始是count数组的第一个索引。

array

Problem explanation:

问题说明:

  1. Make a count array (initialize all elements to 0) to store the bits of each number in input array.

    创建一个计数数组(将所有元素初始化为0),以将每个数字的位存储在输入数组中。

  2. In each iteration access the input number and extract its bits to store in count array for instance if number is 6 which is 110 in binary, so 110 & 1 stores 0 at count[0], then right shift the number to obtain the next bit from left that is 11 & 1 stores 1 at count[1] and similarly again >> number to get 1 again at count[2].

    在每次迭代中,访问输入数字并提取其比特以存储在计数数组中,例如,如果数字为6(二进制数为110),则110&1在count [0]处存储0,然后右移数字以获得下一个比特从11&1的左起在count [1]处存储1,同样>> >>在count [2]处再次获得1。

  3. After each iteration count array is updated. Now since every element except one occurs thrice in input array, therefore bits at every index exist in the form of 3n and 3n +1.

    每次迭代后,更新计数数组。 现在,由于除一个元素外的每个元素都在输入数组中出现三次,因此每个索引处的位都以3n和3n +1的形式存在。

  4. So taking modulus by 3 leaves only unique number's bits in count array as remainders. This will give the binary number of the unique number.

    因此,将模数减3只会在计数数组中保留唯一数字的位数作为余数。 这将给出唯一编号的二进制编号。

  5. Now convert binary to decimal by ∑ multiply (bit with 2^index at respective index).

    现在,将∑乘以将二进制转换为十进制(在相应的索引处具有2 ^ index的位)。

  6. Return the unique number in array.

    返回数组中的唯一编号。

Program:

程序:

#include <iostream>
using namespace std;
int unique(int *arr,int n)
{ 
//array of size 64 for max 64 bit size
int count[64]={0}; 
//count array stores bit of every number
for(int k=0;k<n;k++) 
{ 
int i=0;
int num=arr[k]; 
while(num>0)
{
// extract bit 
count[i]+=(num&1); 
i++;
// right shift to get next leftmost bit
num=num>>1;        
}
}
// starting from first index 2^0=1 
int power=1;  
int result=0;
for(int j=0;j<64;j++)
{
// take modulus of count array by 3
count[j] %=3; 
result+=count[j]*power;
// binary to decimal operation
power=power<<1; 
}
// if there is no unique number 0 is returned
return result; 
}
int main()
{ 
int arr[50];
int n;
cout<<"Enter lenght of the array: ";
cin>>n;
cout<<"Enter array elements..."<<endl;
for(int c=0;c<n;c++)
{
cin>>arr[c];
}
cout<<unique(arr,n)<<" is the unique number in array.";
return 0;
}

Output

输出量

Enter lenght of the array: 4
Enter array elements...
10 10 10 40
40 is the unique number in array.

翻译自: https://www.includehelp.com/cpp-programs/find-unique-number-using-bit-masking.aspx

子网掩码+ip地址

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/545447.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

消息队列的其他实现方式

在 Redis 5.0 之前消息队列的实现方式有很多种,比较常见的除了我们上文介绍的发布订阅模式,还有两种:List 和 ZSet 的实现方式。 List 和 ZSet 的方式解决了发布订阅模式不能持久化的问题,但这两种方式也有自己的缺点,接下来我们一起来了解一下,先从 List 实现消息队列的…

怎样使用orapwd新建口令文件

orapwd是oracle提供的创建口令文件的命令&#xff0c;如果口令文件不见了可以用这个命令重新创建。下面是orapwd命令的一些解释。D:\oracle\ora92\database>orapwdUsage: orapwd file<fname> password<password> entries<users>wherefile - name of passw…

死锁 预防死锁避免死锁_死锁和处理死锁的方法

死锁 预防死锁避免死锁僵局 (Deadlock) In the multiprogramming operating system, there are a number of processing which fights for a finite number of resources and sometimes waiting process never gets a chance to change its state because the resources for wh…

消息队列——发布订阅模式

在 Redis 中提供了专门的类型:Publisher(发布者)和 Subscriber(订阅者)来实现消息队列。 在文章开始之前,先来介绍消息队列中有几个基础概念,以便大家更好的理解本文的内容。 首先,发布消息的叫做发布方或发布者,也就是消息的生产者,而接收消息的叫做消息的订阅方或…

eclipse安装jetty插件

2019独角兽企业重金招聘Python工程师标准>>> 1 eclipse安装jetty插件 由于项目需要&#xff0c;eclipse需要安装jetty插件&#xff0c;从百度搜索的结果基本都是从http://run-jetty-run.googlecode.com/svn/trunk/updatesite获取jetty&#xff0c;国内的网络根本无法…

实战:分布式锁详解与代码

什么是锁? 锁是一种常用的并发控制机制,用于保证一项资源在任何时候只能被一个线程使用,如果其他线程也要使用同样的资源,必须排队等待上一个线程使用完。 锁的示意图,如下所示: 什么是分布式锁? 上面说的锁指的是程序级别的锁,例如 Java 语言中的 synchronized 和 …

编译原理第二章

编译原理第二章2.3_句型的分析&#xff08;1&#xff09;语法树和二义性&#xff08;2&#xff09;短语和句柄&#xff08;规约问题&#xff09;2.5_文法和语言的Chomsky分类&#xff08;1&#xff09;0型文法&#xff08;2&#xff09;1型文法&#xff08;3&#xff09;2型文法…

微软职位内部推荐-Sr. SW Engineer for Azure Networking

微软近期Open的职位:Senior SW EngineerThe world is moving to cloud computing. Microsoft is betting Windows Azure as our cloud computing platform. Important steps have already been taken to virtualize storage and computing through software, increasing agilit…

对称密码和非对称密码体系_密码学类型:对称和不对称

对称密码和非对称密码体系Cryptography is a study of different techniques used for encryption and decryption of the text to convert the plain text into ciphertext and vice-versa. There are many different cryptographic techniques and algorithm which have been…

servlet 和 struts2 同时使用 以及 使用struts2标签库时报错

做网页的时候 想让 servlet 和 struts 都有效。但是在过滤的时候出了点问题&#xff1a;就是 过滤器的*.action 的时候 struts 标签库失效的问题我觉得以下是个不错的 解决方案&#xff1a;转载&#xff1a;http://blog.sina.cn/dpool/blog/s/blog_7d681d490100zbwf.htmlThe St…

编译原理第三章

编译原理第三章3.1_正规文法和状态转换图&#xff08;1&#xff09;构造状态转换图&#xff08;2&#xff09;状态矩阵3.2_有限自动机3.2.1_确定的有限自动机DFA3.2.2_非确定的有限自动机NFA3.3_NFA转换为DFA&#xff08;NFA确定化&#xff09;3.3.1_无ε动作的NFA确定化3.3.2_…

消息队列终极解决方案——Stream(下)

在开始使用消息分组之前,我们必须手动创建分组才行,以下是几个和 Stream 分组有关的命令,我们先来学习一下它的使用。 消息分组命令 创建消费者群组 127.0.0.1:6379> xgroup create mq group1 0-0 OK相关语法: xgroup create stream-key group-key ID其中: mq 为…

ftp上传和下载命令

2019独角兽企业重金招聘Python工程师标准>>> 假设有一目标FTP服务器&#xff0c;IP&#xff1a;123.123.123.123&#xff0c;用户名&#xff1a;ftpname 密码&#xff1a;ftppwd。当前要通过命令行将D:\ftpin目录下的file.doc上传到目标服务器&#xff0c;从服务器下…

String.IsNullOrEmpty()方法以及C#中的示例

C&#xff03;String.IsNullOrEmpty()方法 (C# String.IsNullOrEmpty() Method) String.IsNullOrEmpty() method is a built-in method of String class and it is used to check whether a string is Null or Empty? If string object is not initialized with a correct val…

消息队列终极解决方案——Stream(上)

在 Redis 5.0 Stream 没出来之前,消息队列的实现方式都有着各自的缺陷,例如: 发布订阅模式 PubSub,不能持久化也就无法可靠的保存消息,并且对于离线重连的客户端不能读取历史消息的缺陷;列表实现消息队列的方式不能重复消费,一个消息消费完就会被删除;有序集合消息队列…

《算法导论》学习笔记——快速排序

快速排序 1.快速排序原理 快速排序是一种应用很广泛的排序算法&#xff0c;与归并排序类似&#xff0c;快速排序也采用了分治策略。对于一个待排序的数组A[p...r]进行快速排序&#xff0c;根据分治思想&#xff0c;可以分为如下三个步骤&#xff1a;   - 分解&#xff1a;数组…

JavaTCP连接

输入输出操作 可以这样理解&#xff1a; BufferedReader/BufferedWriter使用三部曲&#xff1a; 服务器操作 import java.io.*; import java.net.ServerSocket; import java.net.Socket;public class Server {private static int port 8002;//设置端口号public static v…

JavaScript中的If和Else语句(香草)

Generally if statements have the following syntax: 通常&#xff0c; if语句具有以下语法&#xff1a; if(condition){Our code;}The condition, here can be anything from the mathematical expression, Boolean, relation operations etc. 条件&#xff0c;这里可以是数…

不创建 sequence 自增字段

drop table t_log;create table t_log (log_id number primary key,user_name varchar2(100),log_date date);--新建一个表&#xff0c;有id 用户名 和 日期字段create or replace trigger tri_logonafter logon on databasebegininsert into t_log values(nvl((select max(…

完整案例:实现延迟队列的两种方法

延迟队列是指把当前要做的事情,往后推迟一段时间再做。 延迟队列在实际工作中和面试中都比较常见,它的实现方式有很多种,然而每种实现方式也都有它的优缺点,接下来我们来看。 延迟队列的使用场景 延迟队列的常见使用场景有以下几种: 超过 30 分钟未支付的订单,将会被取…