📜  单位数字X所需的最小数字总数为N

📅  最后修改于: 2021-05-24 18:02:49             🧑  作者: Mango

给定两个整数NX ,任务是找到总和N为单位数字X的最小整数计数。如果不存在这样的表示形式,则打印-1
例子:

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

  • 获取N单位数字,并检查单位数字为X的数字总和是否达到该数字。
  • 如果可能,检查是否N ? X *(需要加上单位数字X的数字的最小次数以获得总和N )
  • 如果满足上述条件,则需要打印最少的次数,该数字需要加上单位数字X以获得总和N。否则,打印-1。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
  
// Function to calculate and return
// the minimum number of times a number
// with unit digit X needs to be added
// to get a sum N
int check(int unit_digit, int X)
{
    int times, digit;
  
    // Calculate the number of
    // additions required to get unit
    // digit of N
    for (int times = 1; times <= 10;
         times++) {
        digit = (X * times) % 10;
        if (digit == unit_digit)
            return times;
    }
  
    // If unit digit of N
    // cannot be obtained
    return -1;
}
  
// Function to return the minimum
// number required to represent N
int getNum(int N, int X)
{
    int unit_digit;
  
    // Stores unit digit of N
    unit_digit = N % 10;
  
    // Stores minimum addition
    // of X required to
    // obtain unit digit of N
    int times = check(unit_digit, X);
  
    // If unit digit of N
    // cannot be obtained
    if (times == -1)
        return times;
  
    // Otherwise
    else {
  
        // If N is greater than
        // or equal to (X*times)
        if (N >= (times * X))
  
            // Minimum count of numbers
            // that needed to represent N
            return times;
  
        // Representation not
        // possible
        else
            return -1;
    }
}
  
// Driver Code
int main()
{
    int N = 58, X = 7;
    cout << getNum(N, X) << endl;
    return 0;
}


Java
// Java Program to implement
// the above approach
class GFG{
      
// Function to calculate and return
// the minimum number of times a number
// with unit digit X needs to be added
// to get a sum N
static int check(int unit_digit, int X)
{
    int times, digit;
  
    // Calculate the number of
    // additions required to get unit
    // digit of N
    for (times = 1; times <= 10;
                    times++) 
    {
        digit = (X * times) % 10;
        if (digit == unit_digit)
            return times;
    }
  
    // If unit digit of N
    // cannot be obtained
    return -1;
}
  
// Function to return the minimum
// number required to represent N
static int getNum(int N, int X)
{
    int unit_digit;
  
    // Stores unit digit of N
    unit_digit = N % 10;
  
    // Stores minimum addition
    // of X required to
    // obtain unit digit of N
    int times = check(unit_digit, X);
  
    // If unit digit of N
    // cannot be obtained
    if (times == -1)
        return times;
  
    // Otherwise
    else 
    {
  
        // If N is greater than
        // or equal to (X*times)
        if (N >= (times * X))
  
            // Minimum count of numbers
            // that needed to represent N
            return times;
  
        // Representation not
        // possible
        else
            return -1;
    }
}
  
// Driver Code
public static void main(String []args)
{
    int N = 58, X = 7;
    System.out.println( getNum(N, X));
}
}
  
// This code is contributed by Ritik Bansal


Python3
# Python3 program to implement
# the above approach
  
# Function to calculate and return
# the minimum number of times a number
# with unit digit X needs to be added
# to get a sum N
def check(unit_digit, X):
      
    # Calculate the number of additions
    # required to get unit digit of N
    for times in range(1, 11):
        digit = (X * times) % 10
        if (digit == unit_digit):
            return times
              
    # If unit digit of N
    # cannot be obtained
    return -1
  
# Function to return the minimum
# number required to represent N
def getNum(N, X):
      
    # Stores unit digit of N
    unit_digit = N % 10
  
    # Stores minimum addition
    # of X required to
    # obtain unit digit of N
    times = check(unit_digit, X)
  
    # If unit digit of N
    # cannot be obtained
    if (times == -1):
        return times
  
    # Otherwise
    else:
  
        # If N is greater than
        # or equal to (X*times)
        if (N >= (times * X)):
  
            # Minimum count of numbers
            # that needed to represent N
            return times
  
        # Representation not
        # possible
        else:
            return -1
  
# Driver Code
N = 58
X = 7
  
print(getNum(N, X))
  
# This code is contributed by Sanjit_Prasad


C#
// C# Program to implement
// the above approach
using System;
class GFG{
      
// Function to calculate and return
// the minimum number of times a number
// with unit digit X needs to be added
// to get a sum N
static int check(int unit_digit, int X)
{
    int times, digit;
  
    // Calculate the number of
    // additions required to get unit
    // digit of N
    for (times = 1; times <= 10;
                    times++) 
    {
        digit = (X * times) % 10;
        if (digit == unit_digit)
            return times;
    }
  
    // If unit digit of N
    // cannot be obtained
    return -1;
}
  
// Function to return the minimum
// number required to represent N
static int getNum(int N, int X)
{
    int unit_digit;
  
    // Stores unit digit of N
    unit_digit = N % 10;
  
    // Stores minimum addition
    // of X required to
    // obtain unit digit of N
    int times = check(unit_digit, X);
  
    // If unit digit of N
    // cannot be obtained
    if (times == -1)
        return times;
  
    // Otherwise
    else
    {
  
        // If N is greater than
        // or equal to (X*times)
        if (N >= (times * X))
  
            // Minimum count of numbers
            // that needed to represent N
            return times;
  
        // Representation not
        // possible
        else
            return -1;
    }
}
  
// Driver Code
public static void Main()
{
    int N = 58, X = 7;
    Console.Write(getNum(N, X));
}
}
  
// This code is contributed by Code_Mech


输出:
4

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