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&0X01);} void findnumsappearonce (int data[],int length,int* num1,int* num2) {if (data==null| | LENGTH<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,&NUM1,&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