📌  相关文章
📜  除去K位后可能的最大数字

📅  最后修改于: 2021-04-22 06:20:47             🧑  作者: Mango

给定正数N ,目标是找到从N中删除任何K位数字后可形成的最大数。

例子:

方法:

  • 循环K次。
  • 在每次迭代期间,从N的当前值中除去每个数字一次,并存储所获得的所有数字的最大值。
  • 为了实现这一点,我们存储(N /(i * 10))* i +(N%i)的最大值,其中i的范围为[1,10 l – 1 ] ,其中l表示当前值的位数的N。
  • 将此最大值视为N的当前值,然后进行下一个迭代并重复上述步骤。
  • 因此,在每次迭代之后,我们从N的当前值中删除了最少的位数。将过程重复K次后,我们获得了尽可能多的数目。

例如:

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
  
#include 
using namespace std;
  
// Function to return the
// largest number possible
int maxnumber(int n, int k)
{
    // Generate the largest number
    // after removal of the least
    // K digits one by one
    for (int j = 0; j < k; j++) {
  
        int ans = 0;
        int i = 1;
  
        // Remove the least digit
        // after every iteration
        while (n / i > 0) {
  
            // Store the numbers formed after
            // removing every digit once
            int temp = (n / (i * 10))
                           * i
                       + (n % i);
            i *= 10;
  
            // Compare and store the maximum
            ans = max(ans, temp);
        }
  
        // Store the largest
        // number remaining
        n = ans;
    }
  
    // Return the remaining number
    // after K removals
    return n;
}
  
// Driver code
int main()
{
    int n = 6358;
    int k = 1;
  
    cout << maxnumber(n, k) << endl;
    return 0;
}


Java
// Java program to implement
// the above approach
  
import java.util.*;
import java.math.*;
  
class GFG {
  
    // Function to return the
    // largest number possible
    static int maxnumber(int n, int k)
    {
        // Generate the largest number
        // after removal of the least
        // K digits one by one
        for (int j = 0; j < k; j++) {
  
            int ans = 0;
            int i = 1;
  
            // Remove the least digit
            // after every iteration
            while (n / i > 0) {
  
                // Store the numbers formed after
                // removing every digit once
                int temp = (n / (i * 10))
                               * i
                           + (n % i);
                i *= 10;
  
                // Compare and store the maximum
                ans = Math.max(ans, temp);
            }
            n = ans;
        }
  
        // Return the remaining number
        // after K removals
        return n;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n = 6358;
        int k = 1;
  
        System.out.println(maxnumber(n, k));
    }
}


Python3
# Python program to implement
# the above approach
  
def maxnumber(n, k):
# Function to return the
# largest number possible
   
    for i in range(0, k):
        # Generate the largest number 
        # after removal of the least K digits
        # one by one
        ans = 0
        i = 1
        
        while n // i > 0:
        # Remove the least digit 
        # after every iteration
            temp = (n//(i * 10))*i + (n % i)
            i *= 10
        # Store the numbers formed after 
        # removing every digit once
          
        # Compare and store the maximum
            if temp > ans:
                ans = temp
        n = ans        
    
    # Return the remaining number
    # after K removals
    return ans;
    
   
n = 6358
k = 1
print(maxnumber(n, k))


C#
// C# program to implement
// the above approach
  
using System;
  
class GFG {
  
    // Function to return the
    // largest number possible
    static int maxnumber(int n, int k)
    {
        // Generate the largest number
        // after removal of the least
        // K digits one by one
        for (int j = 0; j < k; j++) {
  
            int ans = 0;
            int i = 1;
  
            // Remove the least digit
            // after every iteration
            while (n / i > 0) {
  
                // Store the numbers formed after
                // removing every digit once
                int temp = (n / (i * 10))
                               * i
                           + (n % i);
                i *= 10;
  
                // Compare and store the maximum
                if (temp > ans)
                    ans = temp;
            }
  
            // Store the largest
            // number remaining
            n = ans;
        }
  
        // Return the remaining number
        // after K removals
        return n;
    }
  
    // Driver code
    static public void Main()
    {
        int n = 6358;
        int k = 1;
        Console.WriteLine(maxnumber(n, k));
    }
}


输出:
658