📜  小于或等于给定数的2的最高幂

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

给定数字n,请找到小于或等于n的2的最高幂。
例子 :

Input : n = 10
Output : 8

Input : n = 19
Output : 16

Input : n = 32
Output : 32

一个简单的解决方案是从n开始进行校验并保持递减直到我们找到2的幂。

C++
// C++ program to find highest power of 2 smaller
// than or equal to n.
#include
using namespace std;
 
int highestPowerof2(int n)
{
    int res = 0;
    for (int i=n; i>=1; i--)
    {
        // If i is a power of 2
        if ((i & (i-1)) == 0)
        {
            res = i;
            break;
        }
    }
    return res;
}
 
// Driver code
int main()
{
    int n = 10;
    cout << highestPowerof2(n);
    return 0;
}


Python3
# Python3 program to find highest
# power of 2 smaller than or
# equal to n.
def highestPowerof2(n):
 
    res = 0;
    for i in range(n, 0, -1):
         
        # If i is a power of 2
        if ((i & (i - 1)) == 0):
         
            res = i;
            break;
         
    return res;
 
# Driver code
n = 10;
print(highestPowerof2(n));
     
# This code is contributed by mits


C#
// C# code to find highest power
// of 2 smaller than or equal to n.
using System;
 
class GFG
{
public static int highestPowerof2(int n)
{
    int res = 0;
    for (int i = n; i >= 1; i--)
        {
        // If i is a power of 2
        if ((i & (i - 1)) == 0)
            {
                res = i;
                break;
            }
        }
    return res;
}
     
    // Driver Code
    static public void Main ()
    {
        int n = 10;
        Console.WriteLine(highestPowerof2(n));
    }
}
 
// This code is contributed by ajit


PHP
= 1; $i--)
    {
        // If i is a power of 2
        if (($i & ($i - 1)) == 0)
        {
            $res = $i;
            break;
        }
    }
    return $res;
}
 
// Driver code
$n = 10;
echo highestPowerof2($n);
     
// This code is contributed by m_kit
?>


Javascript


C++
// C++ program to find highest power of 2 smaller
// than or equal to n.
#include
using namespace std;
 
int highestPowerof2(unsigned int n)
{
    // Invalid input
    if (n < 1)
        return 0;
 
    int res = 1;
 
    // Try all powers starting from 2^1
    for (int i=0; i<8*sizeof(unsigned int); i++)
    {
        int curr = 1 << i;
 
        // If current power is more than n, break
        if (curr > n)
           break;
 
        res = curr;
    }
 
    return res;
}
 
// Driver code
int main()
{
    int n = 10;
    cout << highestPowerof2(n);
    return 0;
}


Java
// Java program to find
// highest power of 2 smaller
// than or equal to n.
import java.io.*;
 
class GFG
{
static int highestPowerof2(int n)
{
    // Invalid input
    if (n < 1)
        return 0;
 
    int res = 1;
 
    // Try all powers
    // starting from 2^1
    for (int i = 0; i < 8 * Integer.BYTES; i++)
    {
        int curr = 1 << i;
 
        // If current power is
        // more than n, break
        if (curr > n)
        break;
 
        res = curr;
    }
 
    return res;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 10;
    System.out.println(highestPowerof2(n));
}
}
 
// This code is contributed aj_36


python3
# Python3 program to find highest power of 2 smaller
# than or equal to n.
 
import sys
 
def highestPowerof2( n):
 
    # Invalid input
    if (n < 1):
        return 0
  
    res = 1
  
    #Try all powers starting from 2^1
    for i in range(8*sys.getsizeof(n)):
     
        curr = 1 << i
  
        # If current power is more than n, break
        if (curr > n):
             break
  
        res = curr
  
    return res
  
# Driver code
if __name__ == "__main__":
 
    n = 10
    print(highestPowerof2(n))


C#
// C# program to find
// highest power of 2 smaller
// than or equal to n.
using System;
 
class GFG
{
static int highestPowerof2(int n)
{
    // Invalid input
    if (n < 1)
        return 0;
 
    int res = 1;
 
    // Try all powers
    // starting from 2^1
    for (int i = 0; i < 8 * sizeof(uint); i++)
    {
        int curr = 1 << i;
 
        // If current power is
        // more than n, break
        if (curr > n)
        break;
 
        res = curr;
    }
 
    return res;
}
 
// Driver code
static public void Main ()
{
    int n = 10;
    Console.WriteLine(highestPowerof2(n));
}
}
 
// This code is contributed ajit


PHP
 $n)
        break;
 
        $res = $curr;
    }
 
    return $res;
}
 
// Driver code
$n = 10;
echo highestPowerof2($n);
     
// This code is contributed
// by m_kit
?>


Javascript


C++
// C++ program to find highest power of 2 smaller
// than or equal to n.
#include
using namespace std;
 
int highestPowerof2(int n)
{
   int p = (int)log2(n);
   return (int)pow(2, p);
}
 
// Driver code
int main()
{
    int n = 10;
    cout << highestPowerof2(n);
    return 0;
}


Java
// Java program to find
// highest power of 2
// smaller than or equal to n.
import java.io.*;
 
class GFG
{
static int highestPowerof2(int n)
{
     
    int p = (int)(Math.log(n) /
                  Math.log(2));
    return (int)Math.pow(2, p);
}
 
// Driver code
public static void main (String[] args)
{
    int n = 10;
    System.out.println(highestPowerof2(n));
}
}
 
// This code is contributed
// by m_kit


Python3
# Python3 program to find highest
# power of 2 smaller than or
# equal to n.
import math
 
def highestPowerof2(n):
 
    p = int(math.log(n, 2));
    return int(pow(2, p));
 
# Driver code
n = 10;
print(highestPowerof2(n));
 
# This code is contributed by mits


C#
// C# program to find
// highest power of 2
// smaller than or equal to n.
using System;
 
class GFG
{
static int highestPowerof2(int n)
{
    int p = (int)(Math.Log(n) /
                   Math.Log(2));
    return (int)Math.Pow(2, p);
}
 
// Driver code
static public void Main ()
{
    int n = 10;
    Console.WriteLine(highestPowerof2(n));
}
}
 
// This code is contributed
// by ajit


PHP


输出 :

8

时间复杂度: O(n)。在最坏的情况下,循环运行下(n / 2)次。当n的形式为2 x – 1时,会发生最坏的情况。
一个有效的解决方案是使用按位左移运算符查找从1开始的所有2的幂。对于每个幂,请检查它是否小于或等于n。以下是该想法的实现。

C++

// C++ program to find highest power of 2 smaller
// than or equal to n.
#include
using namespace std;
 
int highestPowerof2(unsigned int n)
{
    // Invalid input
    if (n < 1)
        return 0;
 
    int res = 1;
 
    // Try all powers starting from 2^1
    for (int i=0; i<8*sizeof(unsigned int); i++)
    {
        int curr = 1 << i;
 
        // If current power is more than n, break
        if (curr > n)
           break;
 
        res = curr;
    }
 
    return res;
}
 
// Driver code
int main()
{
    int n = 10;
    cout << highestPowerof2(n);
    return 0;
}

Java

// Java program to find
// highest power of 2 smaller
// than or equal to n.
import java.io.*;
 
class GFG
{
static int highestPowerof2(int n)
{
    // Invalid input
    if (n < 1)
        return 0;
 
    int res = 1;
 
    // Try all powers
    // starting from 2^1
    for (int i = 0; i < 8 * Integer.BYTES; i++)
    {
        int curr = 1 << i;
 
        // If current power is
        // more than n, break
        if (curr > n)
        break;
 
        res = curr;
    }
 
    return res;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 10;
    System.out.println(highestPowerof2(n));
}
}
 
// This code is contributed aj_36

python3

# Python3 program to find highest power of 2 smaller
# than or equal to n.
 
import sys
 
def highestPowerof2( n):
 
    # Invalid input
    if (n < 1):
        return 0
  
    res = 1
  
    #Try all powers starting from 2^1
    for i in range(8*sys.getsizeof(n)):
     
        curr = 1 << i
  
        # If current power is more than n, break
        if (curr > n):
             break
  
        res = curr
  
    return res
  
# Driver code
if __name__ == "__main__":
 
    n = 10
    print(highestPowerof2(n))
   

C#

// C# program to find
// highest power of 2 smaller
// than or equal to n.
using System;
 
class GFG
{
static int highestPowerof2(int n)
{
    // Invalid input
    if (n < 1)
        return 0;
 
    int res = 1;
 
    // Try all powers
    // starting from 2^1
    for (int i = 0; i < 8 * sizeof(uint); i++)
    {
        int curr = 1 << i;
 
        // If current power is
        // more than n, break
        if (curr > n)
        break;
 
        res = curr;
    }
 
    return res;
}
 
// Driver code
static public void Main ()
{
    int n = 10;
    Console.WriteLine(highestPowerof2(n));
}
}
 
// This code is contributed ajit

的PHP

 $n)
        break;
 
        $res = $curr;
    }
 
    return $res;
}
 
// Driver code
$n = 10;
echo highestPowerof2($n);
     
// This code is contributed
// by m_kit
?>

Java脚本


输出 :

8

使用日志的解决方案
感谢Anshuman Jha提出此解决方案。

C++

// C++ program to find highest power of 2 smaller
// than or equal to n.
#include
using namespace std;
 
int highestPowerof2(int n)
{
   int p = (int)log2(n);
   return (int)pow(2, p);
}
 
// Driver code
int main()
{
    int n = 10;
    cout << highestPowerof2(n);
    return 0;
}

Java

// Java program to find
// highest power of 2
// smaller than or equal to n.
import java.io.*;
 
class GFG
{
static int highestPowerof2(int n)
{
     
    int p = (int)(Math.log(n) /
                  Math.log(2));
    return (int)Math.pow(2, p);
}
 
// Driver code
public static void main (String[] args)
{
    int n = 10;
    System.out.println(highestPowerof2(n));
}
}
 
// This code is contributed
// by m_kit

Python3

# Python3 program to find highest
# power of 2 smaller than or
# equal to n.
import math
 
def highestPowerof2(n):
 
    p = int(math.log(n, 2));
    return int(pow(2, p));
 
# Driver code
n = 10;
print(highestPowerof2(n));
 
# This code is contributed by mits

C#

// C# program to find
// highest power of 2
// smaller than or equal to n.
using System;
 
class GFG
{
static int highestPowerof2(int n)
{
    int p = (int)(Math.Log(n) /
                   Math.Log(2));
    return (int)Math.Pow(2, p);
}
 
// Driver code
static public void Main ()
{
    int n = 10;
    Console.WriteLine(highestPowerof2(n));
}
}
 
// This code is contributed
// by ajit

的PHP


输出 :

8

应用问题:
有些人排长队。选择过程遵循以下规则:选择站立在均匀位置上的人。在所选择的人员中形成队列,并再次从这些仅处于偶数位置的人员中选择人员。这一直持续到我们只剩下一个人为止。找出该人在原始队列中的位置。
打印剩下的那个人的位置(原始队列)。
例子 :

Input: n = 10
Output:8
Explanation : 
1 2 3 4 5 6 7 8 9 10  ===>Given queue
    2 4 6 8 10
       4 8
        8

Input: n = 17
Input: 16
Explanation : 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17  ===>Given queue
        2 4 6 8 10 12 14 16
            4 8 12 16
              8 16
               16