给定两个正整数N和K ,以及一个整数X (最初为1 ),任务是找到执行以下操作之一N次后可以获得的X的最小值:
- 将X的值增加K 。
- 将X的当前值加倍。
例子:
Input: N = 4, K = 3, X = 1
Output: 10
Explanation:
Following operations are performed:
Operation 1: Double the value of X from 1 to 2.
Operation 2: Double the value of X from 2 to 4.
Operation 3: Add the value K(= 3) to X modifies the value from 4 to 7.
Operation 4: Add the value K(= 3) to X modifies the value from 7 to 10.
After performing the above operations N(= 4) number of times, the value of X is 10, which is minimum among all possible combinations of operations performed.
Input: N = 7, K = 4, X = 1
Output: 24
方法:可以使用贪心方法解决给定的问题。这个想法是重复执行给定的操作之一,直到执行第二个操作本地最大化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)