简体   繁体   English

给定数字,则打印具有相同数字的下一个最高数字

[英]given a number print the next highest number which has same number of ones

given a number we have to find next highest number and previous number which has same number of 1 bits in their binary representation 给定一个数字,我们必须找到下一个最高数字和前一个数字,它们的二进制表示形式具有相同的1位数字

my solution is a brute force approach ie incrementing the number and counting number of ones if it has same number of 1's then print it 我的解决方案是蛮力方法,即增加数量并计数1(如果它具有相同的1),然后打印出来

for previous number same approach as above one 对于以前的数字,与上述方法相同

is there any optimised solution instead of checking all the numbers? 有没有优化的解决方案而不是检查所有数字?

To get the next highest number, simply search for the first instance of 01 , starting on the right and change this to 10 . 要获得下一个最高的数字,只需从右边开始搜索01的第一个实例,然后将其更改为10 Then collapse all the 1 s to the right of the swapped bits into their least significant positions. 然后将交换位右侧的所有1 s折叠到其最低有效位置。

Example: 例:

 ||
00110000 // 0x30 in hex

After swap: 交换后:

 ||
01010000 // 0x50 in hex

Next, collapse all the 1s to the right of the swapped bits into the least significant positions: 接下来,将交换位右侧的所有1折叠到最低有效位置:

 ||---->
01000001 // 0x41 in hex

To get the previous number, search for the first instance of 10 starting from the right, and replace it with 01 . 要获取前一个数字,请从右侧开始搜索10的第一个实例,并将其替换为01 Then collapse all the 0 s after the swapped bits in to the least significant positions (alternatively, collapse all the 1 s after the swapped bits into their most significant positions). 然后将交换位之后的所有0 s折叠到最低有效位置(或者,将交换位后的所有1 s折叠到其最高有效位置)。

Example: 例:

    ||
01001001 // 0x48 in hex

After swap: 交换后:

    ||
01000101 // 0x45 in hex

Next, collapse all the 0s to the right of the swapped bits into the least significant positions: 接下来,将交换位右侧的所有0折叠到最低有效位置:

    ||->
01000110 // 0x46 in hex

Here is a short program that will display both the next higher and lower numbers. 这是一个简短的程序,将显示下一个更高和更低的数字。

#include <iostream>
#include <bitset>
#include <stdlib.h>

using namespace std;
#define NUMSIZE 8       // The number of bits to display

// Finds the first and second values, swaps them, and collapses.
void swap_and_collapse(bitset<NUMSIZE>& num, bool v1, bool v2, bool c) {
  // Find the v1 immediately followed by v2 in the number.
  int insert_pos = 0;
  for (int i=1; i<NUMSIZE; i++) {
    if ((num.test(i-1) == v2) && (num.test(i) == v1)) {
      // Found them.  Swap the values.
      num.flip(i-1); num.flip(i);
      break;
    } else {
      // Move any c bits to the beginning.
      if (num.test(i-1) == c) {
        num.flip(i-1);
        num.flip(insert_pos++);
      } // if (num.test(i-1) == c) 
    } // if ((num.test(i-1) == v2) && (num.test(i) == v1)) 
  } // for (int i=0; i<16; i++)
} // swap_and_collapse

int main(int argc, char* argv[]) {
  // Get the number from the command line and display in binary.
  int value = atoi(argv[1]);
  bitset<NUMSIZE> orig  (value);
  cout << "Original Number " << orig.to_ulong() << " is " << orig << endl;

  // Find the next higher number.
  bitset<NUMSIZE> higher(value);
  swap_and_collapse(higher, 0, 1, 1);
  // Find the next lower number.
  bitset<NUMSIZE> lower (value);
  swap_and_collapse(lower,  1, 0, 0);

  cout << "Higher number   " << higher.to_ulong() << " is " << higher << endl;
  cout << "Lower number    " <<  lower.to_ulong() << " is " <<  lower << endl;
} // main

here is the code to find the next highest number with equal number of ones 这是查找与位数相等的下一个最高数字的代码

#include<iostream>
using namespace std;
int main()
{
int c;
cin>>c;
int n=c;
int c1=0;
while(((c&1)==0) && (c!=0) )
{
    c1++;
    c=c>>1;
}
cout<<"the no of zereos"<<c1<<endl;
cout<<c<<endl;
int c2=0;
while((c&1)==1)
{
    c2++;
    c=c>>1;
}
cout<<"no of ones"<<c2<<endl;
cout<<c;
int p=c1+c2;
n=n|(1<<p);
int a=1<<p;
int b=a-1;
cout<<endl<<b;
int mask=~b;
n=n&mask;
a=1<<(c2-1);
b=a-1;
n=n|b;
cout<<"\n"<<a;
cout<<"\n"<<n;

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM