📜  要添加到 N 的最小数字,使其不包含数字 D

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

要添加到 N 的最小数字,使其不包含数字 D

给定一个正整数N和一个数字D ,任务是找到需要添加到给定数字上的最小非负数,这样在相加后,结果数字不应包含数字D

例子:

方法:这个问题可以通过从右到左迭代给定数字的数字并找到不包含数字D的最近数字来解决。

请按照以下步骤操作:

  • 迭代给定的数字并检查给定的数字是否存在于数字中。
  • 如果当前数字与给定数字匹配,则检查以下条件
    • 如果当前数字在1 到 9的范围内,则将当前数字递增1并将其右侧的所有数字设为 0
    • 否则,如果当前数字为0 ,则将当前数字1 ,并将其所有数字设为 1
  • 使用上述条件形成的数字与原始数字之间的差是所需的最小数字。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
 
#include 
using namespace std;
 
// Function to calculate smallest positive
// number to be added to remove
// the given digit
int smallestNumber(int n, int d)
{
    // Copy the number to another variable
    int temp = n;
 
    int ans = 0, count = 0;
 
    // Iterate over digits of the given
    // number from right to left
    while (temp > 0) {
 
        // Extract the last digit of the
        // number and check if it is
        // equal to the given digit (d)
        int remainder = temp % 10;
        temp = temp / 10;
        count++;
 
        if (remainder == d) {
            // If the digit which
            // need to be replaced
            // is equal to 0 then
            // increment the current
            // digit and make the all digits
            // to its right 1
            if (d == 0) {
 
                string num = string(count, '1');
            
                temp = temp * pow(10, count) + stoi(num);
            }
 
            // Else, increment the current digit
            // by 1 and make the all digits
            // to its right 0
            else {
 
                temp = temp * pow(10, count)
                       + (remainder + 1)
                             * pow(10, count - 1);
            }
            // After the removal of given
            // digit the number will be temp
            // So, the required smallest
            // number is (temp - n)
            ans = temp - n;
 
            // Set count as 0
            count = 0;
        }
    }
 
    // Return the result
    return ans;
}
 
// Driver code
int main()
{
    int N = 100;
    int D = 0;
 
    // print the smallest number required
    // to be added to remove
    // the digit 'D' in number 'N'
    cout << smallestNumber(N, D) << "\n";
 
    return 0;
}


Java
// Java implementation of the above approach
class GFG{
 
// Function to calculate smallest positive
// number to be added to remove
// the given digit
public static int smallestNumber(int n, int d)
{
     
    // Copy the number to another variable
    int temp = n;
 
    int ans = 0, count = 0;
 
    // Iterate over digits of the given
    // number from right to left
    while (temp > 0)
    {
         
        // Extract the last digit of the
        // number and check if it is
        // equal to the given digit (d)
        int remainder = temp % 10;
        temp = temp / 10;
        count++;
 
        if (remainder == d)
        {
             
            // If the digit which need to be
            // replaced is equal to 0 then
            // increment the current digit
            // and make the all digits to
            // its right 1
            if (d == 0)
            {
                String num = "";
                for(int i = 0; i < count; i++)
                {
                    num = num + "1";
                }
                temp = (int)(temp * Math.pow(10, count) +
                       Integer.parseInt(num));
            }
 
            // Else, increment the current digit
            // by 1 and make the all digits
            // to its right 0
            else
            {
                temp = (int) (temp * Math.pow(10, count) +
                   (remainder + 1) * Math.pow(10, count - 1));
            }
             
            // After the removal of given
            // digit the number will be temp
            // So, the required smallest
            // number is (temp - n)
            ans = temp - n;
 
            // Set count as 0
            count = 0;
        }
    }
 
    // Return the result
    return ans;
}
 
// Driver code
public static void main(String args[])
{
    int N = 100;
    int D = 0;
 
    // Print the smallest number required
    // to be added to remove the digit 'D'
    // in number 'N'
    System.out.println(smallestNumber(N, D));
}
}
 
// This code is contributed by _saurabh_jaiswal


Python3
# Python 3 implementation of the above approach
 
# Function to calculate smallest positive
# number to be added to remove
# the given digit
def smallestNumber(n, d):
 
    # Copy the number to another variable
    temp = n
 
    ans = 0
    count = 0
 
    # Iterate over digits of the given
    # number from right to left
    while (temp > 0):
 
        # Extract the last digit of the
        # number and check if it is
        # equal to the given digit (d)
        remainder = temp % 10
        temp = temp // 10
        count += 1
 
        if (remainder == d):
            # If the digit which
            # need to be replaced
            # is equal to 0 then
            # increment the current
            # digit and make the all digits
            # to its right 1
            if (d == 0):
 
                num = '1'*count
                temp = temp * pow(10, count) + int(num)
 
            # Else, increment the current digit
            # by 1 and make the all digits
            # to its right 0
            else:
 
                temp = (temp * pow(10, count) + (remainder + 1)
                             * pow(10, count - 1))
 
            # After the removal of given
            # digit the number will be temp
            # So, the required smallest
            # number is (temp - n)
            ans = temp - n
 
            # Set count as 0
            count = 0
 
    # Return the result
    return ans
 
# Driver code
if __name__ == "__main__":
 
    N = 100
    D = 0
 
    # print the smallest number required
    # to be added to remove
    # the digit 'D' in number 'N'
    print(smallestNumber(N, D))
 
    # This code is contributed by ukasp.


C#
// C# implementation for the above approach
using System;
 
class GFG {
 
  // Function to calculate smallest positive
  // number to be added to remove
  // the given digit
  public static int smallestNumber(int n, int d)
  {
 
    // Copy the number to another variable
    int temp = n;
 
    int ans = 0, count = 0;
 
    // Iterate over digits of the given
    // number from right to left
    while (temp > 0)
    {
 
      // Extract the last digit of the
      // number and check if it is
      // equal to the given digit (d)
      int remainder = temp % 10;
      temp = temp / 10;
      count++;
 
      if (remainder == d)
      {
 
        // If the digit which need to be
        // replaced is equal to 0 then
        // increment the current digit
        // and make the all digits to
        // its right 1
        if (d == 0)
        {
          string num = "";
          for(int i = 0; i < count; i++)
          {
            num = num + "1";
          }
          temp = (int)(temp * Math.Pow(10, count) +
                       Int32.Parse(num));
        }
 
        // Else, increment the current digit
        // by 1 and make the all digits
        // to its right 0
        else
        {
          temp = (int) (temp * Math.Pow(10, count) +
                        (remainder + 1) * Math.Pow(10, count - 1));
        }
 
        // After the removal of given
        // digit the number will be temp
        // So, the required smallest
        // number is (temp - n)
        ans = temp - n;
 
        // Set count as 0
        count = 0;
      }
    }
 
    // Return the result
    return ans;
  }
 
  // Driver code
  public static void Main(String[] args)
  {
 
    int N = 100;
    int D = 0;
 
    // Print the smallest number required
    // to be added to remove the digit 'D'
    // in number 'N'
    Console.Write(smallestNumber(N, D));
  }
}
 
// This code is contributed by sanjoy_62.


Javascript


输出
11

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