28.46% 1000ms 65536K
Given an array of AA, the remaining numbers appear three times, except for one occurrence. Find the number that appears once.
For example: \{1, 2, 1, 2, 1, 2, 7\}{1,2,1,2,1,2,7}, find 77.
Your algorithm can only be linear time complexity, and can not use the extra space Oh ~ input Format
The first line enters a number n (1 \leq n \leq) n (1≤n≤500), which represents the length of the array.
The next line is entered in the range of nn int ( -2147483648\ldots 2147483647−2147483648 ...). 2147483647 An integer representing the array AA. Ensure that the input array is legitimate. output Format
Outputs an integer that represents only the number of occurrences in an array. Sample Input
4
0 0 0 5
Sample Output
5
int binary system under a total of 32 digits, each occurrence of 3, statistics can be 32 bit more than 3 0, if only once, the occurrence of more than 3 of 1, so through the statistics of each number of digits, to determine what number of more.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
int main ()
{
int N;
while (Cin>>n)
{
int com[32]= {};
while (n--)
{
int temp;
cin>>temp;
for (int i=0; i<32; i++)
com[i]+= ((temp>>i) &1);
int pow2=1,ans=0;
for (int i=0; i<32; i++)
{
ans+=pow2* (com[i]%3);
pow2*=2;
}
cout<<ans<<endl;
}
return 0;
}