📌  相关文章
📜  通过在极端位置交换位来最大化给定的无符号数。

📅  最后修改于: 2021-05-04 13:22:35             🧑  作者: Mango

给定一个数字,可以通过在其极端位置(即第一个和最后一个位置,第二个和第二个最后一个位置)交换位来最大化它。
例子:

Input : 4 (0000...0100)
Output : 536870912 (0010...0000)
In the above example swapped 3rd and 3rd last
bit to maximize the given unsigned number.

Input : 12 (00000...1100)
Output : 805306368 (0011000...0000)

In the above example 3rd and 3rd last bit and
4th and 4th last bit are swapped to maximize 
the given unsigned number.

天真的方法:
1.将数字转换为位表示并将其存储在数组中。
2.如果位表示形式的较低有效位大于较高有效位,即从两端遍历数组,即如果较低有效位为1且较高有效位为0,则交换它们,否则不采取任何措施。
3.将获得的二进制表示形式转换回数字。
高效方法:
1.创建原始号码的副本,因为原始号码将被修改,因此迭代地获取极端位置的位。
2.如果低位有效位是1,高位有效位是0,则仅从位交换该位中的位,继续执行该过程,直到低位有效位的位置小于高位有效位的位置。
3.显示最大数量。

C++
// C++ program to find maximum number by
// swapping extreme bits.
#include 
using namespace std;
 
#define ull unsigned long long int
 
ull findMax(ull num)
{
    ull num_copy = num;
 
    /* Traverse bits from both extremes */
    int j = sizeof(unsigned long long int) * 8 - 1;
    int i = 0;
    while (i < j) {
 
        // Obtaining i-th and j-th bits
        int m = (num_copy >> i) & 1;
        int n = (num_copy >> j) & 1;
 
        /* Swapping the bits if lesser significant
           is greater than higher significant
           bit and accordingly modifying the number */
        if (m > n) {
            int x = (1 << i | 1 << j);
            num = num ^ x;
        }
 
        i++;
        j--;
    }
    return num;
}
 
// Driver code
int main()
{
    ull num = 4;
    cout << findMax(num);
    return 0;
}


Java
// Java program to find maximum number by
// swapping extreme bits.
 
class GFG {
 
    static int findMax(int num) {
        byte size_of_int = 4;
        int num_copy = num;
 
        /* Traverse bits from both extremes */
        int j = size_of_int * 8 - 1;
        int i = 0;
        while (i < j) {
 
            // Obtaining i-th and j-th bits
            int m = (num_copy >> i) & 1;
            int n = (num_copy >> j) & 1;
 
            /* Swapping the bits if lesser significant
        is greater than higher significant
        bit and accordingly modifying the number */
            if (m > n) {
                int x = (1 << i | 1 << j);
                num = num ^ x;
            }
 
            i++;
            j--;
        }
        return num;
    }
 
    // Driver code
    static public void main(String[] args) {
        int num = 4;
        System.out.println(findMax(num));
    }
}
 
// This code is contributed by 29AjayKumar


Python 3
# Python 3 program to find maximum number
# by swapping extreme bits.
 
def findMax( num):
    num_copy = num
 
    # Traverse bits from both extremes
    j = 4 * 8 - 1;
    i = 0
    while (i < j) :
 
        # Obtaining i-th and j-th bits
        m = (num_copy >> i) & 1
        n = (num_copy >> j) & 1
 
        # Swapping the bits if lesser significant
        # is greater than higher significant
        # bit and accordingly modifying the number
        if (m > n) :
            x = (1 << i | 1 << j)
            num = num ^ x
 
        i += 1
        j -= 1
    return num
 
# Driver code
if __name__ == "__main__":
     
    num = 4
    print(findMax(num))
 
# This code is contributed by ita_c


C#
// C# program to find maximum number by
// swapping extreme bits.
using System;
public class GFG {
  
    static int findMax(int num) {
        byte size_of_int = 4;
        int num_copy = num;
  
        /* Traverse bits from both extremes */
        int j = size_of_int * 8 - 1;
        int i = 0;
        while (i < j) {
  
            // Obtaining i-th and j-th bits
            int m = (num_copy >> i) & 1;
            int n = (num_copy >> j) & 1;
  
            /* Swapping the bits if lesser significant
        is greater than higher significant
        bit and accordingly modifying the number */
            if (m > n) {
                int x = (1 << i | 1 << j);
                num = num ^ x;
            }
  
            i++;
            j--;
        }
        return num;
    }
  
// Driver code
    static public void Main() {
        int num = 4;
        Console.Write(findMax(num));
    }
}
  
// This code is contributed by 29AjayKumar


Javascript


输出:

536870912