📌  相关文章
📜  获得N所需的等于2的幂的最小硬币数量

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

给定整数N ,任务是找到要更改N美分所需的2 i形式的最小硬币数。

例子:

天真的方法:解决此问题的最简单方法是将硬币的所有可能值存储在数组中,并使用动态编程打印N分钱所需的最少硬币计数。
时间复杂度: O(N 2 )
辅助空间: O(N)

高效方法:可以利用以下事实来优化上述方法:可以以2 s的幂的形式表示任何数字。想法是对N的设置位进行计数并打印获得的计数。请按照以下步骤解决问题:

  • 遍历N的二进制表示形式中的位,并检查当前位是否已设置。如果发现为真,则增加计数。
  • 最后,打印获得的总数。

下面是上述方法的实现:

C++
// C++ program for above approach
#include 
using namespace std;
  
// Function to count of set bit in N
void count_setbit(int N)
{
  
    // Stores count of set bit in N
    int result = 0;
  
    // Iterate over the range [0, 31]
    for (int i = 0; i < 32; i++) {
  
        // If current bit is set
        if ((1 << i) & N) {
  
            // Update result
            result++;
        }
    }
    cout << result << endl;
}
  
// Driver Code
int main()
{
    int N = 43;
  
    count_setbit(N);
    return 0;
}


C
// C program for above approach
#include 
  
// Function to count of set bit in N
void count_setbit(int N)
{
  
    // Stores count of set bit in N
    int result = 0;
  
    // Iterate over the range [0, 31]
    for (int i = 0; i < 32; i++) {
  
        // If current bit is set
        if ((1 << i) & N) {
  
            // Update result
            result++;
        }
    }
    printf("%d\n", result);
}
  
// Driver Code
int main()
{
    int N = 43;
  
    count_setbit(N);
    return 0;
}


Java
// Java program for above approach
public class Main {
  
    // Function to count of set bit in N
    public static void count_setbit(int N)
    {
        // Stores count of set bit in N
        int result = 0;
  
        // Iterate over the range [0, 31]
        for (int i = 0; i < 32; i++) {
  
            // If current bit is set
            if (((1 << i) & N) > 0) {
  
                // Update result
                result++;
            }
        }
  
        System.out.println(result);
    }
  
    // Driver Code
    public static void main(String[] args)
    {
  
        int N = 43;
        count_setbit(N);
    }
}


Python
# Python program for above approach
  
# Function to count of set bit in N
def count_setbit(N):
  
    # Stores count of set bit in N
    result = 0
      
    # Iterate over the range [0, 31]
    for i in range(32):
      
       # If current bit is set   
       if( (1 << i) & N ):
             
           # Update result
           result = result + 1
      
    print(result)
  
if __name__ == '__main__':
  
    N = 43
    count_setbit(N)


C#
// C# program for above approach
using System;
class GFG {
  
    // Function to count of setbit in N
    static void count_setbit(int N)
    {
  
        // Stores count of setbit in N
        int result = 0;
  
        // Iterate over the range [0, 31]
        for (int i = 0; i < 32; i++) {
  
            // If current bit is set
            if (((1 << i) & N) > 0) {
  
                // Update result
                result++;
            }
        }
  
        Console.WriteLine(result);
    }
  
    // Driver Code
    static void Main()
    {
  
        int N = 43;
        count_setbit(N);
    }
}


输出:
4

时间复杂度: O(log 2 (N))
辅助空间: O(1)