/*
找出数组中两种出现奇数次的数
*/
#include<iostream>
using namespace std;
int find_odd_number1(int *array,int length) //第一轮异或,所有数异或
{
int num = 0;
int i = 0;
while(i<length)
{
num = num ^ array[i];
i++;
}
return num;
}
int find_odd_number2(int *array,int length,int rightOne)
{
int num = 0;
int i = 0;
while(i<length)
{
if(array[i]&rightOne!=0) //满足这个条件说明这一位不是1
{
num = num ^ array[i];
}
i++;
}
return num;
}
int main()
{
int arr[]= {1, 1, 5, 4, 4, 5, 5, 12, 23, 12,12, 23};
int n = sizeof(arr) / sizeof(arr[0]);
int eor1 = find_odd_number1(arr,n);
int rightOne = eor1 & (~eor1 + 1); //提出最右边的1
int a = find_odd_number2(arr, n, rightOne);
int b = a ^ eor1;
cout <<"the first number is "<<" "<<a<<endl<<"the second number is"<<" "<<b<<endl;
}
找到数组中出现奇数次的两种数
最新推荐文章于 2022-08-25 17:58:10 发布