📜  通过在任意位置插入给定数字来最大化数字 N

📅  最后修改于: 2022-05-13 01:56:07.174000             🧑  作者: Mango

通过在任意位置插入给定数字来最大化数字 N

给定一个正整数N和一个数字K ,任务是通过将给定数字K插入其中N来找到给定数字N的最大值。

例子:

方法:给定的问题可以通过在下一位小于K的位置插入K来解决。 请按照以下步骤解决问题:

  • 初始化两个字符串S ,通过将给定的数字N类型转换为字符串并将字符串结果“”以存储在其中插入K后的最大可能数字。
  • 初始化两个变量,将L设为字符串S的长度,将i设为0 
  • 遍历字符串S直到K小于S[i]并将字符S[i]附加到字符串result
  • 现在将K附加到结果中,然后将字符串的所有剩余字符附加到结果中。
  • 完成上述步骤后,打印字符串结果和最大可能数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum value
// of N after inserting the digit K
void maximizeNumber(int N, int K)
{
    // Convert it into N to string
    string s = to_string(N);
    int L = s.length();
 
    // Stores the maximum value of N
    // after inserting K
    string result;
    int i = 0;
 
    // Iterate till all digits that
    // are not less than K
    while ((i < L) && (K <= (s[i] - '0'))) {
 
        // Add the current digit to
        // the string result
        result.push_back(s[i]);
        ++i;
    }
 
    // Add digit 'K' to result
    result.push_back(char(K + '0'));
 
    // Iterate through all remaining
    // characters
    while (i < L) {
 
        // Add current digit to result
        result.push_back(s[i]);
        ++i;
    }
 
    // Print the maximum number formed
    cout << result;
}
 
// Driver Code
int main()
{
    int N = 6673, K = 6;
    maximizeNumber(N, K);
 
    return 0;
}


Java
// Java program for the above approach
 
class GFG {
 
    // Function to find the maximum value
    // of N after inserting the digit K
public static void maximizeNumber(int N, int K)
{
    // Convert it into N to string
    String s = Integer.toString(N);
    int L = s.length();
 
    // Stores the maximum value of N
    // after inserting K
    String result =  "";
    int i = 0;
 
    // Iterate till all digits that
    // are not less than K
    while ((i < L) && (K <= ((int)s.charAt(i) - (int)'0'))) {
 
        // Add the current digit to
        // the string result
        result += (s.charAt(i));
        ++i;
    }
 
    // Add digit 'K' to result
    result += ((char)(K + (int)'0'));
 
    // Iterate through all remaining
    // characters
    while (i < L) {
 
        // Add current digit to result
        result += (s.charAt(i));
        ++i;
    }
 
    // Print the maximum number formed
    System.out.println(result);
}
 
    // Driver Code
    public static void main (String args[]) {
        int N = 6673, K = 6;
        maximizeNumber(N, K);
    }
 
}
 
// This code is contributed by _saurabh_Jaiswal.


Python3
# Python 3 program for the above approach
 
# Function to find the maximum value
# of N after inserting the digit K
def maximizeNumber(N, K):
 
    # Convert it into N to string
    s = str(N)
    L = len(s)
 
    # Stores the maximum value of N
    # after inserting K
    result = ""
    i = 0
 
    # Iterate till all digits that
    # are not less than K
    while ((i < L) and (K <= (ord(s[i]) - ord('0')))):
 
        # Add the current digit to
        # the string result
        result += (s[i])
        i += 1
 
    # Add digit 'K' to result
    result += (chr(K + ord('0')))
 
    # Iterate through all remaining
    # characters
    while (i < L):
 
        # Add current digit to result
        result += (s[i])
        i += 1
 
    # Print the maximum number formed
    print(result)
 
 
# Driver Code
if __name__ == "__main__":
 
    N = 6673
    K = 6
    maximizeNumber(N, K)
 
    # This code is contributed by ukasp.


C#
// C# program for above approach
using System;
 
class GFG{
 
    // Function to find the maximum value
    // of N after inserting the digit K
public static void maximizeNumber(int N, int K)
{
    // Convert it into N to string
    String s = N.ToString();
    int L = s.Length;
 
    // Stores the maximum value of N
    // after inserting K
    string result =  "";
    int i = 0;
 
    // Iterate till all digits that
    // are not less than K
    while ((i < L) && (K <= ((int)s[i]- (int)'0'))) {
 
        // Add the current digit to
        // the string result
        result += (s[i]);
        ++i;
    }
 
    // Add digit 'K' to result
    result += ((char)(K + (int)'0'));
 
    // Iterate through all remaining
    // characters
    while (i < L) {
 
        // Add current digit to result
        result += (s[i]);
        ++i;
    }
 
    // Print the maximum number formed
     Console.Write(result);
}
// Driver Code
static void Main()
{
     
    int N = 6673, K = 6;
    maximizeNumber(N, K);
}
}
 
// This code is contributed by sanjoy_62.


Javascript


输出:
66763

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