子网掩码+ip地址_C ++程序使用位掩码查找唯一编号
子網(wǎng)掩碼+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個數(shù)字的數(shù)組中查找唯一數(shù)字,其中除一個(唯一數(shù)字)外其余所有其余三次。
Constraints: n<10^5
約束: n <10 ^ 5
Example:
例:
Input:101 2 3 2 4 1 2 3 1 3Output:4Solution: 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個其余的所有數(shù)組都具有3的多重性。 例如,4用二進制表示為100.從最左邊的位開始是count數(shù)組的第一個索引。
Problem explanation:
問題說明:
Make a count array (initialize all elements to 0) to store the bits of each number in input array.
創(chuàng)建一個計數(shù)數(shù)組(將所有元素初始化為0),以將每個數(shù)字的位存儲在輸入數(shù)組中。
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].
在每次迭代中,訪問輸入數(shù)字并提取其比特以存儲在計數(shù)數(shù)組中,例如,如果數(shù)字為6(二進制數(shù)為110),則110&1在count [0]處存儲0,然后右移數(shù)字以獲得下一個比特從11&1的左起在count [1]處存儲1,同樣>> >>在count [2]處再次獲得1。
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.
每次迭代后,更新計數(shù)數(shù)組。 現(xiàn)在,由于除一個元素外的每個元素都在輸入數(shù)組中出現(xiàn)三次,因此每個索引處的位都以3n和3n +1的形式存在。
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.
因此,將模數(shù)減3只會在計數(shù)數(shù)組中保留唯一數(shù)字的位數(shù)作為余數(shù)。 這將給出唯一編號的二進制編號。
Now convert binary to decimal by ∑ multiply (bit with 2^index at respective index).
現(xiàn)在,將∑乘以將二進制轉換為十進制(在相應的索引處具有2 ^ index的位)。
Return the unique number in array.
返回數(shù)組中的唯一編號。
Program:
程序:
#include <iostream> using namespace std;int unique(int *arr,int n) { //array of size 64 for max 64 bit sizeint count[64]={0}; //count array stores bit of every numberfor(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 bitnum=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 3count[j] %=3; result+=count[j]*power;// binary to decimal operationpower=power<<1; }// if there is no unique number 0 is returnedreturn 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
子網(wǎng)掩碼+ip地址
總結
以上是生活随笔為你收集整理的子网掩码+ip地址_C ++程序使用位掩码查找唯一编号的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 回文子序列_计算回文子序列的总数
- 下一篇: 硬核|定时任务的10种实现方案,满足你的