📌  相关文章
📜  将数字N最多减少D,以最大程度增加尾随9的数量

📅  最后修改于: 2021-04-29 01:09:33             🧑  作者: Mango

给定两个正整数ND ,任务是将N的值最多减D ,以使N包含尾随9 s的最大计数。

例子:

天真的方法:解决此问题的最简单方法是使用变量i[0,D]范围内进行迭代,并将N的值减i 。最后,打印N的值,该值包含尾随9的最大可能计数。
时间复杂度: O(D * log 10 (N))
辅助空间: O(1)

高效的方法:可以基于以下观察来优化上述方法:

请按照以下步骤解决问题:

  • 初始化一个变量,例如res,以存储N的递减值,其最大末尾计数为9
  • 初始化一个变量,例如cntDigits ,将数字的计数存储在N中。
  • 使用变量i遍历[1,cntDigits]范围并检查N%pow(10,i)是否大于或等于D。如果发现为真,则输出res的值。
  • 否则,将res更新为N%pow(10,i)– 1

下面是上述方法的实现:

C++
// CPP program for the above approach
#include 
#define ll long long int
using namespace std;
   
// Function to find a number with
// maximum count of trailing nine
void maxNumTrailNine(int n, int d)
{
    int res = n;
   
    // Stores count of digits in n
    int cntDigits = log10(n) + 1;
   
    // Stores power of 10
    int p10 = 10;
   
    for (int i = 1; i <= cntDigits; i++) {
   
        // If last i digits greater than
        // or equal to d
        if (n % p10 >= d) {
            break;
        }
   
        else {
   
            // Update res
            res = n - n % p10 - 1;
        }
   
        // Update p10
        p10 = p10 * 10;
    }
    cout << res;
}
   
// Driver Code
int main()
{
    int n = 1025, d = 6;
   
    // Function Call
    maxNumTrailNine(n, d);
}


Java
// Java program for the above approach
class GFG
{
         
    // Function to find a number with
    // maximum count of trailing nine
    static void maxNumTrailNine(int n, int d)
    {
        int res = n;
       
        // Stores count of digits in n
        int cntDigits = (int)Math.log10(n) + 1;
       
        // Stores power of 10
        int p10 = 10;
       
        for (int i = 1; i <= cntDigits; i++)
        {
       
            // If last i digits greater than
            // or equal to d
            if (n % p10 >= d)
            {
                break;
            }
       
            else
            {
       
                // Update res
                res = n - n % p10 - 1;
            }
       
            // Update p10
            p10 = p10 * 10;
        }
        System.out.println(res);
    }
   
    // Driver Code
    public static void main (String[] args)
    {
        int n = 1025, d = 6;
       
        // Function Call
        maxNumTrailNine(n, d);
    }
}
 
// This code is contribute by AnkThon


Python3
# Python3 program for the above approach
from math import log10
 
# Function to find a number with
# maximum count of trailing nine
def maxNumTrailNine(n, d):
    res = n
 
    # Stores count of digits in n
    cntDigits = int(log10(n) + 1)
 
    # Stores power of 10
    p10 = 10
 
    for i in range(1, cntDigits + 1):
 
        # If last i digits greater than
        # or equal to d
        if (n % p10 >= d):
            break
 
        else:
 
            # Update res
            res = n - n % p10 - 1
 
        # Update p10
        p10 = p10 * 10
 
    print (res)
 
# Driver Code
if __name__ == '__main__':
    n, d = 1025, 6
 
    # Function Call
    maxNumTrailNine(n, d)
 
    # This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to find a number with
  // maximum count of trailing nine
  static void maxNumTrailNine(int n, int d)
  {
    int res = n;
 
    // Stores count of digits in n
    int cntDigits = (int)Math.Log10(n) + 1;
 
    // Stores power of 10
    int p10 = 10;     
    for (int i = 1; i <= cntDigits; i++)
    {
 
      // If last i digits greater than
      // or equal to d
      if (n % p10 >= d)
      {
        break;
      }
 
      else
      {
 
        // Update res
        res = n - n % p10 - 1;
      }
 
      // Update p10
      p10 = p10 * 10;
    }
    Console.WriteLine(res);
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int n = 1025, d = 6;
 
    // Function Call
    maxNumTrailNine(n, d);
  }
}
 
 
// This code contributed by shikhasingrajput


输出:
1019

时间复杂度: O(min(log 10 (D),log 10 (N))
辅助空间: O(1)