Mainly familiar bit operation, looking for a number binary 1 bits the same, and the size of the most similar number.
//Title Description////with a positive integer, find the two numbers that have the same number and the closest size to the second binary representation of 1. (one slightly larger, one slightly smaller)//given a positive integer int x, return a vector that represents the two number (small in front) of the request. Ensure that the answer exists. //Test Examples://2//return: [1, 4]////Ideas://get a slightly larger number://C0 is the number of trailing 0, C1 is adjacent to the trailing 0 left consecutive bits of 1, p is the rightmost but not trailing 0 equals C0 + C1//1 position p to 1//2 put P right all position zero//(The quick way to do this is to change the trailing 0 to 1, then add 1)//3 Insert C1-1 1 on the right//get a slightly smaller number://1 C1 is the number of trailing 1, C0 is adjacent to the left of the trailing 1 a series of 0 number, p is the rightmost but not trailing 1 equals C0 + C1//2 position p to 0//(The fast version is the trailing 1 is set to 0, then minus 1)//3 put P right so position clear 0//Insert C1 + 1 1 on the right-close of P#include<iostream>using namespacestd; #include<vector>#include<math.h>classclosenumber{ Public: Vector<int> Getclosenumber (intx) {//Write code hereVector <int>result; intC01 =0;//number of trailing 0 intC11 =0;//Trailing 0 left is a total of 1 consecutive numbers intC1 =0;//number of trailing 1 intC0 =0;//Trailing 1 Number of digits to the left all 0 intc = x, d = x;//Temp Variable//Find Max separately while((C &1) ==0) && (c! =0) ) {C01++; C>>=1; } while(C &1) ==1) {C11++; C>>=1; } //min while(D &1) ==1) {C1++; D>>=1; } while((D &1) ==0) && (d! =0) ) {C0++; D>>=1; } result.push_back ((x- (1<< (C1))-(1<< (C0-1)) +1));//minResult.push_back ((x + (1<< (C11-1)) + (1<< c01)-1));//Max returnresult; } intFindNext (inta) { //C0 is the number of tail 0, C1 is 0 to the left all 1 of the number /*1, the index position p is set to 1, 2, the index bit from 0 to P-1 0 (the fast way is to change the trailing 0 to 1, then add 1) 3, the index bit 0 to the c1-2 position of 1 */ intC0 =0, C1 =0; inttemp =A; while(Temp &1) ==0&& Temp! =0) {C0++; Temp>>=1; } while(Temp &1) ==1) {C1++; Temp>>=1; } //to determine whether there is no greater number if(C0 + C1 = = to|| C0 + C1 = =0){ return-1; } intp = C0 + C1;//Index is the position of P, at which point the position of P must be 0 .//The fast versionA + = POW (2, C0);//2^p-2^c1-....//3, Position 0 to C1-2 1A + = POW (2, C1-1) -1; returnA; } intFindpre (intb) { //C1 is the number of tail 1, C0 is the number of the tail 1 left all 0 /*1, the index position p clear 2, the P right all the bits are set to 1 (the fast version is the trailing 1 to 0, then minus 1) 3, the bit 0 in place c0-2 clear 0*/ intC1 =0, C0 =0; inttemp =b; while(Temp &1) ==1) {C1++; Temp>>=1; } while(Temp &1) ==0&& Temp! =0) {C0++; Temp>>=1; } intp = C0 + C1;//P-bit must be 1// theB-= POW (2, C1)-1; b-=1; b-= POW (2, C0-1) -1; returnb; }};intMain () {Closenumber C; Vector<int> Vc=c.getclosenumber ( -); return 0;}
The two number of the closest size (bit operation)