给定一个数,通过在其极限位置(即第一个和最后一个位置、第二个和第二个最后一个位置等)交换位来最大化它。 例如:
null
Input : 4 (0000...0100)Output : 536870912 (0010...0000)In the above example swapped 3rd and 3rd lastbit to maximize the given unsigned number.Input : 12 (00000...1100)Output : 805306368 (0011000...0000)In the above example 3rd and 3rd last bit and4th 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 <bits/stdc++.h> 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
<script> // JavaScript program to find maximum number by // swapping extreme bits. function findMax(num) { let num_copy = num; /* Traverse bits from both extremes */ let j = 4 * 8 - 1; let i = 0; while (i < j) { // Obtaining i-th and j-th bits let m = (num_copy >> i) & 1; let 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) { let x = (1 << i | 1 << j); num = num ^ x; } i++; j--; } return num; } // Driver code let num = 4; document.write(findMax(num)); // This code is contributed by Manoj. </script> |
输出:
536870912
本文由 阿迪蒂亚·古普塔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END