📌  相关文章
📜  通过从 1 开始重复与 K 或 2 正好 N 次相乘的最小数字

📅  最后修改于: 2021-10-26 05:30:20             🧑  作者: Mango

给定两个正整数NK ,以及一个整数X最初为1 ),任务是找到执行以下操作之一N次后可以获得的X的最小值:

  • X的值增加K
  • X的当前值加倍。

例子:

方法:可以使用贪心方法解决给定的问题。这个想法是重复执行给定的操作之一,直到执行第二个操作本地最大化X的值。请按照以下步骤解决问题:

  • 迭代范围[1, N]并执行以下步骤:
    • 如果X的值小于K则将X的值更新为X*2
    • 否则,将X的值增加K
  • 完成上述步骤后,打印为X的最小可能值X的值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum value
// of X after increment X by K or twice
// value of X in each of N operations
int minPossibleValue(int N, int K, int X)
{
    // Iterate over the range [1, N]
    for (int i = 1; i <= N; i++) {
 
        // If the value of X is less
        // than equal to K
        if (X <= K) {
            X = X * 2;
        }
 
        // Otherwise
        else {
            X = X + K;
        }
    }
 
    // Return the minimum value of X
    return X;
}
 
// Driver Code
int main()
{
    int N = 7, K = 4, X = 1;
    cout << minPossibleValue(N, K, X);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Function to find the minimum value
// of X after increment X by K or twice
// value of X in each of N operations
public static int minPossibleValue(int N, int K,
                                   int X)
{
     
    // Iterate over the range [1, N]
    for(int i = 1; i <= N; i++)
    {
         
        // If the value of X is less
        // than equal to K
        if (X <= K)
        {
            X = X * 2;
        }
 
        // Otherwise
        else
        {
            X = X + K;
        }
    }
 
    // Return the minimum value of X
    return X;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 7, K = 4, X = 1;
     
    System.out.println(minPossibleValue(N, K, X));
}
}
 
// This code is contributed by Potta Lokesh


Python3
# Python program for the above approach
 
# Function to find the minimum value
# of X after increment X by K or twice
# value of X in each of N operations
def minPossibleValue(N, K, X):
     
    # Iterate over the range [1, N]
    for i in range(1, N + 1):
       
        # If the value of X is less
        # than equal to K
        if (X <= K):
            X = X * 2;
 
        # Otherwise
        else :
            X = X + K;
 
    # Return the minimum value of X
    return X;
 
# Driver Code
N = 7;
K = 4;
X = 1;
 
print(minPossibleValue(N, K, X));
 
# This code is contributed by _saurabh_jaiswal


C#
// C# program for the above approach
using System;
 
class GFG {
 
    // Function to find the minimum value
    // of X after increment X by K or twice
    // value of X in each of N operations
    static int minPossibleValue(int N, int K, int X)
    {
 
        // Iterate over the range [1, N]
        for (int i = 1; i <= N; i++) {
 
            // If the value of X is less
            // than equal to K
            if (X <= K) {
                X = X * 2;
            }
 
            // Otherwise
            else {
                X = X + K;
            }
        }
 
        // Return the minimum value of X
        return X;
    }
 
    // Driver Code
    public static void Main()
    {
        int N = 7, K = 4, X = 1;
 
        Console.WriteLine(minPossibleValue(N, K, X));
    }
}
 
// This code is contributed by subham348.


Javascript


输出:
24

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