[Offoffoffoffer] number that appears only once in the 9-degree OJ Array

Source: Internet
Author: User
Question: The number that appears only once in the array

Time Limit: 1 second

Memory limit: 32 MB

Description:
In an integer array, all numbers except two appear twice. Write a program to find the numbers that appear only once.
Input:
Each test case contains two rows: the first row contains an integer N, indicating the array size. 2 <= n <= 10 ^ 6. The second row contains N integers, indicating the array elements, all of which are Int.
Output:
For each test case, only two numbers appear in the output array. The order of output numbers from small to large.
Sample input:
82 4 3 6 3 2 5 5
Sample output:
4 6

Analysis: if there is only one number in the array that appears only once, and the other number appears twice, then all the numbers are operated or different to obtain the number that appears only once. The problem is that there are two numbers that only appear once in the array, so we can divide the original array into two groups, each group contains a number that appears only once and a number of pairs that appear twice. Then, the two groups are divided into different or groups, respectively, and two numbers appear only once.

Procedure:

In the first step, all elements in the original array are exclusive or must have a non-zero number, and this number is obtained by two numbers that only appear once.

Step 2: Find the first bit position from the low position to the high position, and use 1 in this position to group, that is, all the elements in the original array whose position is 1 are a group, all the elements at this position 0 are a group.

Step 3: perform an exclusive or operation on the two arrays to obtain the two numbers that appear only once in the array.

# Include <stdio. h> # include <stdlib. h> int A [1000001]; int min (int A, int B) {return a <B? A: B;} int max (int A, int B) {return A> B? A: B;} int findfirstbit1 (INT N) {int COUNT = 1; while (! (N & 1) {n = n> 1; count ++;} return count;} int main () {int temp, I, n, B1, B2, num; while (scanf ("% d", & N )! =-1) {// use an exclusive or operation. Finally, temp is the value of temp = 0; for (I = 0; I <n; I ++) {scanf ("% d", & A [I]); temp = temp ^ A [I];} // search from low to high in temp, find the first bit that appears 1, and record the position num = findfirstbit1 (temp); // split the array into two parts, each part contains a number that appears only once and several pairs that appear twice. // It is equivalent to exclusive or to two arrays respectively. Finally, the B1 and B2 values are the two numbers that appear only once in the array. B1 = b2 = 0; for (I = 0; I <n; I ++) {If (A [I]> (num-1) & 1 = 1) b1 = b1 ^ A [I]; elseb2 = b2 ^ A [I];} printf ("% d \ n", min (b1, b2 ), max (b1, b2);} return 0 ;}

Note: This program can pass through C ++ on the 9-degree OJ, but uses C to determine timeout.

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.