Sword refers to the offer series source code-only one occurrence of the number in the array

Source: Internet
Author: User

Topic 1351: Digital time limit of only one occurrence in an array: 1 seconds Memory limit: 32 Mega Special: No submission: 2582 Resolution: 758 title Description: In an integer array, except for two digits, the other numbers appear two times. Please write the program to find the two only occurrences of the number. Input: Each test case consists of two lines: the first row contains an integer n, which indicates the size of the array. 2<=n <= 10^6. The second row contains n integers, which represent the array elements and the elements are int. Output: corresponding to each test case, the output array only appears once in two digits. The output of the numbers from small to large order. Sample input: 82 4 3 6 3 2 5 5 Sample output: 4 6


#include <iostream> #include <stdio.h>using namespace std;unsigned int findFirstBitIs1 (int num) {int Indexbi    t = 0;        while (((num&0x01) ==0) && (indexbit<8*sizeof (int))) {num=num>>1;    indexbit++; } return indexbit;}    BOOL isBit1 (int num, int indexbit) {num = num>>indexbit; return (NUM&AMP;0X01);} void findnumsappearonce (int data[],int length,int* num1,int* num2) {if (data==null| |      LENGTH&LT;2) return;      int resultexclusiveor = 0;      for (int i=0;i<length;i++) {resultexclusiveor^=data[i];    } unsigned int pos = FINDFIRSTBITIS1 (Resultexclusiveor);    *num1=*num2=0;        for (int j=0;j<length;j++) {if (IsBit1 (Data[j],pos)) {*num1^=data[j];        }else{*num2^=data[j];    }}}int Main () {int n;        while (scanf ("%d", &n)!=eof) {int* data = new Int[n];        int num1=0,num2=0;        for (int i=0;i<n;i++) {scanf ("%d", &data[i]);       } Findnumsappearonce (DATA,N,&AMP;NUM1,&AMP;NUM2);        int Min,max;        Min = num1<num2?num1:num2;        max = num1>num2?num1:num2;    printf ("%d%d\n", Min,max); } return 0;}


Sword refers to the offer series source code-only one occurrence of the number in the array

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.