Garlic ———— A separate number

Source: Internet
Author: User
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;
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.