📜  通过加2的幂来形成数字X的最低成本

📅  最后修改于: 2021-04-21 23:44:28             🧑  作者: Mango

给定一个由N个整数组成的数组arr []和一个整数X。数组中的元素arr [i]表示使用2 i的成本。任务是找到最小成本,以选择加到X上的数字。

例子:

方法:可以使用基本的动态编程解决问题。这里使用了每个数字都可以使用2的幂形成的事实。我们最初可以计算形成2的幂的最小成本。重复发生如下:

重新计算数组后,我们可以简单地根据数字X中设置的位数继续增加成本。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the minimum cost
int MinimumCost(int a[], int n, int x)
{
  
    // Re-compute the array
    for (int i = 1; i < n; i++) {
        a[i] = min(a[i], 2 * a[i - 1]);
    }
  
    int ind = 0;
  
    int sum = 0;
  
    // Add answers for set bits
    while (x) {
  
        // If bit is set
        if (x & 1)
            sum += a[ind];
  
        // Increase the counter
        ind++;
  
        // Right shift the number
        x = x >> 1;
    }
  
    return sum;
}
  
// Driver code
int main()
{
    int a[] = { 20, 50, 60, 90 };
    int x = 7;
    int n = sizeof(a) / sizeof(a[0]);
    cout << MinimumCost(a, n, x);
  
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
  
class GFG 
{
      
// Function to return the minimum cost
static int MinimumCost(int a[], int n, int x)
{
  
    // Re-compute the array
    for (int i = 1; i < n; i++) 
    {
        a[i] = Math.min(a[i], 2 * a[i - 1]);
    }
  
    int ind = 0;
  
    int sum = 0;
  
    // Add answers for set bits
    while (x > 0) 
    {
  
        // If bit is set
        if (x != 0 )
            sum += a[ind];
  
        // Increase the counter
        ind++;
  
        // Right shift the number
        x = x >> 1;
    }
  
    return sum;
}
  
// Driver code
public static void main (String[] args) 
{
  
    int a[] = { 20, 50, 60, 90 };
    int x = 7;
    int n =a.length;
    System.out.println (MinimumCost(a, n, x));
}
}
  
// This Code is contributed by akt_mit


Python3
# Python 3 implementation of the approach
  
# Function to return the minimum cost
def MinimumCost(a, n, x):
      
    # Re-compute the array
    for i in range(1, n, 1):
        a[i] = min(a[i], 2 * a[i - 1])
  
    ind = 0
  
    sum = 0
  
    # Add answers for set bits
    while (x):
          
        # If bit is set
        if (x & 1):
            sum += a[ind]
  
        # Increase the counter
        ind += 1
  
        # Right shift the number
        x = x >> 1
  
    return sum
  
# Driver code
if __name__ == '__main__':
    a = [20, 50, 60, 90]
    x = 7
    n = len(a)
    print(MinimumCost(a, n, x))
  
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
  
class GFG 
{
      
// Function to return the minimum cost
public static int MinimumCost(int []a, int n, int x)
{
  
    // Re-compute the array
    for (int i = 1; i < n; i++) 
    {
        a[i] = Math.Min(a[i], 2 * a[i - 1]);
    }
  
    int ind = 0;
  
    int sum = 0;
  
    // Add answers for set bits
    while (x > 0) 
    {
  
        // If bit is set
        if (x != 0 )
            sum += a[ind];
  
        // Increase the counter
        ind++;
  
        // Right shift the number
        x = x >> 1;
    }
  
    return sum;
}
  
// Driver code
public static void Main () 
{
  
    int []a = { 20, 50, 60, 90 };
    int x = 7;
    int n =a.Length;
    Console.WriteLine(MinimumCost(a, n, x));
}
}
  
// This Code is contributed by SoM15242


PHP
> 1;
    }
  
    return $sum;
}
  
    // Driver code
    $a = array( 20, 50, 60, 90 );
    $x = 7;
    $n = sizeof($a) / sizeof($a[0]);
    echo MinimumCost($a, $n, $x);
  
// This code is contributed by ajit.
?>


输出:
120