📌  相关文章
📜  查找具有n个置位和m个未置位的最大数字

📅  最后修改于: 2021-05-04 21:08:56             🧑  作者: Mango

给定两个非负数nm 。问题是要找到最大数量的二进制表示形式,该数量具有n个设置位和m个未设置位。
注意:在二进制表示形式中,前1(或最左1)位之前的0位被计数
限制条件: 1 <= n,0 <= m,(m + n)<= 31
例子 :

Input : n = 2, m = 2
Output : 12
(12)10 = (1100)2
We can see that in the binary representation of 12 
there are 2 set and 2 unsets bits and it is the largest number. 

Input : n = 4, m = 1
Output : 30

步骤如下:

  1. 计算num =(1 <<(n + m))–1。这将产生一个具有(n + m)个位数的数字num ,并且全部被设置。
  2. 现在,切换num的最后m位,然后返回切换后的数字。请参阅这篇文章。
C++
// C++ implementation to find the largest number
// with n set and m unset bits
#include 
 
using namespace std;
 
// function to toggle the last m bits
unsigned int toggleLastMBits(unsigned int n,
                            unsigned int m)
{
    // if no bits are required to be toggled
    if (m == 0)
        return n;
 
    // calculating a number 'num' having 'm' bits
    // and all are set
    unsigned int num = (1 << m) - 1;
 
    // toggle the last m bits and return the number
    return (n ^ num);
}
 
// function to find the largest number
// with n set and m unset bits
unsigned int largeNumWithNSetAndMUnsetBits(unsigned int n,
                                        unsigned int m)
{
    // calculating a number 'num' having '(n+m)' bits
    // and all are set
    unsigned int num = (1 << (n + m)) - 1;
 
    // required largest number
    return toggleLastMBits(num, m);
}
 
// Driver program to test above
int main()
{
    unsigned int n = 2, m = 2;
    cout << largeNumWithNSetAndMUnsetBits(n, m);
    return 0;
}


Java
// Java implementation to find the largest number
// with n set and m unset bits
import java.io.*;
 
class GFG
{
    // Function to toggle the last m bits
    static int toggleLastMBits(int n, int m)
    {
        // if no bits are required to be toggled
        if (m == 0)
            return n;
  
        // calculating a number 'num' having 'm' bits
        // and all are set
        int num = (1 << m) - 1;
  
        // toggle the last m bits and return the number
        return (n ^ num);
    }
  
    // Function to find the largest number
    // with n set and m unset bits
    static int largeNumWithNSetAndMUnsetBits(int n, int m)
    {
        // calculating a number 'num' having '(n+m)' bits
        // and all are set
        int num = (1 << (n + m)) - 1;
  
        // required largest number
        return toggleLastMBits(num, m);
    }
     
    // driver program
    public static void main (String[] args)
    {
        int n = 2, m = 2;
        System.out.println(largeNumWithNSetAndMUnsetBits(n, m));
    }
}
 
// Contributed by Pramod Kumar


Python3
# Python implementation to
# find the largest number
# with n set and m unset bits
 
# function to toggle
# the last m bits
def toggleLastMBits(n,m):
 
    # if no bits are required
    # to be toggled
    if (m == 0):
        return n
 
    # calculating a number
    # 'num' having 'm' bits
    # and all are set
    num = (1 << m) - 1
 
    # toggle the last m bits
    # and return the number
    return (n ^ num)
 
 
# function to find
# the largest number
# with n set and m unset bits
def largeNumWithNSetAndMUnsetBits(n,m):
 
    # calculating a number
    # 'num' having '(n+m)' bits
    # and all are set
    num = (1 << (n + m)) - 1
 
    # required largest number
    return toggleLastMBits(num, m)
 
# Driver code
 
n = 2
m = 2
 
print(largeNumWithNSetAndMUnsetBits(n, m))
 
# This code is contributed
# by Anant Agarwal.


C#
// C# implementation to find the largest number
// with n set and m unset bits
using System;
 
class GFG
{
    // Function to toggle the last m bits
    static int toggleLastMBits(int n, int m)
    {
        // if no bits are required to be toggled
        if (m == 0)
            return n;
 
        // calculating a number 'num' having 'm' bits
        // and all are set
        int num = (1 << m) - 1;
 
        // toggle the last m bits and return the number
        return (n ^ num);
    }
 
    // Function to find the largest number
    // with n set and m unset bits
    static int largeNumWithNSetAndMUnsetBits(int n, int m)
    {
        // calculating a number 'num' having '(n+m)' bits
        // and all are set
        int num = (1 << (n + m)) - 1;
 
        // required largest number
        return toggleLastMBits(num, m);
    }
     
    // Driver program
    public static void Main ()
    {
        int n = 2, m = 2;
        Console.Write(largeNumWithNSetAndMUnsetBits(n, m));
    }
 
}
 
// This code is contributed by Sam007


PHP


Javascript


输出 :

12