通过递增 X 或乘以 Y,以 K 步从 0 生成最大的 N 位数字
给定整数 N、K、X 和 Y。任务是从 0 开始在 K 步中找到最大可能的 N 位数。使用下面给出的操作:
- 将值增加 X,或
- 将值与 Y 相乘
例子:
Input: N = 2, K = 5, X = 2, Y = 3
Output: 72
Explanation: Sequence would be {0->2->6->8->24->72}
Input: N = 2, K = 10, X = 2, Y = 3
Output: 98
Explanation: Sequence would be
{0 -> 0 -> 0 -> 2 -> 6 -> 8 -> 10 -> 30 -> 32 -> 96 -> 98}.
Notice that the above mentioned sequence 0 is multiplied by 3 two times.
Another possible sequence is
{0 -> 0 -> 2 -> 4 -> 6 -> 8 -> 10 -> 30 -> 32 -> 96 -> 98}.
Input: N = 3 and K = 4
Output: -1
Explanation: Not possible to create 3-digit number in 4 steps
方法:可以使用递归的概念来解决问题。请按照以下步骤操作:
- 对于每个递归步骤,在以下情况下退出递归调用:
- 如果所采取的步数大于 K ,则必须停止递归。
- 如果当前数字的位数超过 N ,则无需在该分支上搜索。
- 如果当前数字等于可以生成的最大 N 位数,则无需进一步处理。它将减少额外的递归调用次数。
- 任何时候可能产生的最大可能的N位数是(10 N – 1) 。
- 现在递归调用当前数字增加X和当前步骤增加1的方法。
- 进行另一个递归调用,当前数字乘以Y并且当前步长增加1 。
- 将两个结果存储在一个变量中。
- 在任何递归点,返回存储在变量中的两个结果中的最大值。
下面是上述方法的实现
C++
// C++ code to implement the approach
#include
using namespace std;
// Function for generating largest
// N-digit number in K-steps.
int largestNDigitNumber(int N, int currentStep,
int K, int X, int Y,
int currentValue)
{
// Further processing is useless
// ifcCurrent value already becomes equal
// to largest N-digit number that
// can be generated
if (currentValue == pow(10, N) - 1)
return currentValue;
// Return 0 steps taken is greater than K
// and also if N or K equal to Zero.
if (currentStep > K || N < 1 || K < 1)
return 0;
// currentValue exceeds maxValue,
// so there is no need to
// keep searching on that branch.
if (currentValue >= pow(10, N))
return 0;
// Recursive calls
int result2 = largestNDigitNumber(
N, currentStep + 1, K, X, Y,
currentValue + X);
int result1 = largestNDigitNumber(
N, currentStep + 1, K, X, Y,
currentValue * Y);
// If both the results are zero
// it means maximum is reached.
if (result1 == 0 && result2 == 0)
return currentValue;
// Checking for maximum of the two results.
return result1 > result2 ? result1 : result2;
}
// Driver code
int main()
{
int N = 2, K = 10, X = 2, Y = 3;
int largest = largestNDigitNumber(
N, 0, K, X, Y, 0);
// Checking whether the returned result
// is a N-digit number or not.
if (largest < pow(10, (N - 1))) {
cout << ("-1");
}
else
cout << largest;
return 0;
}
Java
// Java code to implement the approach
import java.util.*;
class GFG
{
// Function for generating largest
// N-digit number in K-steps.
static int largestNDigitNumber(int N, int currentStep,
int K, int X, int Y,
int currentValue)
{
// Further processing is useless
// ifcCurrent value already becomes equal
// to largest N-digit number that
// can be generated
if (currentValue == Math.pow(10, N) - 1)
return currentValue;
// Return 0 steps taken is greater than K
// and also if N or K equal to Zero.
if (currentStep > K || N < 1 || K < 1)
return 0;
// currentValue exceeds maxValue,
// so there is no need to
// keep searching on that branch.
if (currentValue >= Math.pow(10, N))
return 0;
// Recursive calls
int result2 = largestNDigitNumber(
N, currentStep + 1, K, X, Y,
currentValue + X);
int result1 = largestNDigitNumber(
N, currentStep + 1, K, X, Y,
currentValue * Y);
// If both the results are zero
// it means maximum is reached.
if (result1 == 0 && result2 == 0)
return currentValue;
// Checking for maximum of the two results.
return result1 > result2 ? result1 : result2;
}
// Driver code
public static void main(String[] args)
{
int N = 2, K = 10, X = 2, Y = 3;
int largest = largestNDigitNumber(
N, 0, K, X, Y, 0);
// Checking whether the returned result
// is a N-digit number or not.
if (largest < Math.pow(10, (N - 1))) {
System.out.print("-1");
}
else
System.out.print(largest);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python code for the above approach
# Function for generating largest
# N-digit number in K-steps.
def largestNDigitNumber(N, currentStep, K, X, Y, currentValue):
# Further processing is useless
# ifcCurrent value already becomes equal
# to largest N-digit number that
# can be generated
if (currentValue == 10 ** N - 1):
return currentValue
# Return 0 steps taken is greater than K
# and also if N or K equal to Zero.
if (currentStep > K or N < 1 or K < 1):
return 0
# currentValue exceeds maxValue,
# so there is no need to
# keep searching on that branch.
if (currentValue >= 10 ** N):
return 0
# Recursive calls
result2 = largestNDigitNumber( N, currentStep + 1, K, X, Y, currentValue + X)
result1 = largestNDigitNumber( N, currentStep + 1, K, X, Y, currentValue * Y)
# If both the results are zero
# it means maximum is reached.
if (result1 == 0 and result2 == 0):
return currentValue
# Checking for maximum of the two results.
return result1 if result1 > result2 else result2
# Driver code
N = 2
K = 10
X = 2
Y = 3
largest = largestNDigitNumber(N, 0, K, X, Y, 0)
# Checking whether the returned result
# is a N-digit number or not.
if (largest < 10 ** (N - 1)):
print("-1")
else:
print(largest)
# This code is contributed by Saurabh Jaiswal.
C#
// C# code to implement the approach
using System;
class GFG {
// Function for generating largest
// N-digit number in K-steps.
static int largestNDigitNumber(int N, int currentStep,
int K, int X, int Y,
int currentValue)
{
// Further processing is useless
// ifcCurrent value already becomes equal
// to largest N-digit number that
// can be generated
if (currentValue == Math.Pow(10, N) - 1)
return currentValue;
// Return 0 steps taken is greater than K
// and also if N or K equal to Zero.
if (currentStep > K || N < 1 || K < 1)
return 0;
// currentValue exceeds maxValue,
// so there is no need to
// keep searching on that branch.
if (currentValue >= Math.Pow(10, N))
return 0;
// Recursive calls
int result2 = largestNDigitNumber(
N, currentStep + 1, K, X, Y, currentValue + X);
int result1 = largestNDigitNumber(
N, currentStep + 1, K, X, Y, currentValue * Y);
// If both the results are zero
// it means maximum is reached.
if (result1 == 0 && result2 == 0)
return currentValue;
// Checking for maximum of the two results.
return result1 > result2 ? result1 : result2;
}
// Driver code
public static void Main(string[] args)
{
int N = 2, K = 10, X = 2, Y = 3;
int largest = largestNDigitNumber(N, 0, K, X, Y, 0);
// Checking whether the returned result
// is a N-digit number or not.
if (largest < Math.Pow(10, (N - 1))) {
Console.Write("-1");
}
else
Console.Write(largest);
}
}
// This code is contributed by ukasp.
Javascript
输出
98
时间复杂度: O(2 K )
辅助空间: O(1)