1 /*************************************************************************2 > File name:38_numbersappearonce.cpp3 > Author:juntaran4 > Mail: [email protected]5 > Created time:2016 September 03 Saturday 10:50 32 seconds6 ************************************************************************/7 8#include <stdio.h>9 Ten //Judging the number binary from right to left the index bit is not 1 One BOOLIsbit (intNumintindex) A { -num = num >>index; - return(Num &1); the } - - //all numbers appear two times, only two appear once, find out the two numbers - voidFindnumsappearonce (int* Nums,intLengthint* NUM1,int*num2) + { - if(Nums==null | | length<=2) + return; A at inttemp = nums[0]; - //first time XOR or - for(inti =1; i < length; ++i) -Temp ^=Nums[i]; - - //Right-to- left look for temp binary number 1 in intindex =0; - while(Temp &1) ==0) to { +temp = Temp >>1; -++index; the } * $ Panax Notoginseng*NUM1 =0; -*num2 =0; the + //Group XOR or A for(inti =0; i < length; ++i) the { + if(Isbit (nums[i], index)) -*num1 ^=Nums[i]; $ Else $*num2 ^=Nums[i]; - } - } the - intMain ()Wuyi { the intNums[] = {2,4,3,6,3,2,5,5}; - intLength =8; Wu intNUM1 =0; - intnum2 =0; AboutFindnumsappearonce (nums, Length, &NUM1, &num2); $printf"Num1 is%d\nnum2 is%d\n", NUM1, num2); - return 0; -}
Sword refers to Offer36 array all numbers appear two times, only two appear once, find out these two numbers