Find two numbers that appear only once

Source: Internet
Author: User
Preface I had a weekend with a couple of university friends. I was very happy that everyone was very sensible and working hard. I have graduated from Yahoo for two years, but I have no question about salary, haha, I had a good chat, but I didn't write much code over the weekend. Here I have a few questions on the 9du OJ, Which I recorded by the way (PS: it seems I have recorded this question before, no matter what) question
Description: In an integer array, all the numbers except two appear twice. Write a program to find the numbers that appear only once. Input: the first line of the input contains an integer N (1 <=n <= 1000 ). The next row contains N integers. Output: multiple groups of test data may exist. For each group of data, find the two numbers that appear only once in this array. The order of output numbers is from small to large. Sample input: 62 3 9 3 7 2 sample output: 7 9
Here we mainly examine the bitwise operations of C and introduce the nature of two exclusive or operations:
  • Any number is different or it is equal to 0.
  • Any number (except 0) is equal to itself.
Therefore, we can consider the following ideas:
  • Returns the result of two numbers that appear only once.
  • Because the two numbers are definitely different, the difference or result is definitely not all 0. Find the first position with 1 and mark it as Loc
  • Determine whether the LOC bit of each number in the array is 1 and divide the array into two parts:
  • The two parts of the array are exclusive or
AC code
# Include <stdio. h> # include <stdlib. h> int firstone_loc (INT num) {int I; for (I = 0; num % 2 = 0; I ++) {num = num> 1 ;} return I;} int judge_loc (INT num, int LOC) {num = num> LOC; If (Num % 2 = 1) {return 1 ;} else {return 0 ;}} int main (void) {int I, n, unique, First, Second, Loc, flag, * arr; while (scanf ("% d ", & N )! = EOF) {arr = (int *) malloc (sizeof (INT) * n); for (I = unique = 0; I <n; I ++) {scanf ("% d", arr + I); unique ^ = arr [I];} // The first 1-bit loc = firstone_loc (unique ); for (I = first = Second = 0; I <n; I ++) {flag = judge_loc (ARR [I], Loc); If (FLAG) {First ^ = arr [I];} else {second ^ = arr [I] ;}} if (first <second) {printf ("% d \ n ", first, second);} else {printf ("% d \ n", second, first);} Free (ARR);} return 0 ;} /*************************************** * *********************** problem: 1256 User: wangzhengyi language: C result: accepted time: 570 MS memory: 912 KB ************************************** **************************/

I am not familiar with the bitwise operation, because I have determined whether the I-th bit is 1 and whether % 2 is 1. If I am familiar with the bitwise operation, directly & 1 can follow the AC code of the above method

# Include <stdio. h> # include <stdlib. h> int firstone_loc (INT num) {int I; for (I = 0; (Num & 1) = 0; I ++) {num = num> 1 ;} return I;} int judge_loc (INT num, int LOC) {num = num> LOC; Return (Num & 1) ;}int main (void) {int I, n, unique, First, Second, Loc, flag, * arr; while (scanf ("% d", & N )! = EOF) {arr = (int *) malloc (sizeof (INT) * n); for (I = unique = 0; I <n; I ++) {scanf ("% d", arr + I); unique ^ = arr [I];} // The first 1-bit loc = firstone_loc (unique ); for (I = first = Second = 0; I <n; I ++) {flag = judge_loc (ARR [I], Loc); If (FLAG) {First ^ = arr [I];} else {second ^ = arr [I] ;}} if (first <second) {printf ("% d \ n ", first, second);} else {printf ("% d \ n", second, first);} Free (ARR);} return 0 ;} /*************************************** * *********************** problem: 1256 User: wangzhengyi language: C result: accepted time: 560 MS memory: 912 KB ************************************** **************************/
We can see that bitwise operations are 10 ms faster than the remainder operation.

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.