📌  相关文章
📜  具有 K 作为获得总和 N 所需的最后一位的最小数字的计数

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

具有 K 作为获得总和 N 所需的最后一位的最小数字的计数

给定一个正整数N和一个数字K ,任务是找到以数字K结尾的最小数字计数,使得这些数字的总和为N 。如果不存在总和为K的数字,则打印“-1”

例子:

方法:给定的问题可以通过观察来解决,如果一个数字可以表示为以数字K结尾的数字的总和,那么结果将是最大值 10。按照以下步骤解决问题:

  • 如果数字K是偶数而整数N是奇数,则打印“ -1” ,因为不可能获得偶数的奇数和。
  • 对于数字K ,在[0, 9]范围内找到以数字i结尾的最小数字。如果不可能,则将值设置为INT_MAX
  • 此外,对于每个数字K ,找到在[0, 9]范围内创建以数字i结尾的数字所需的最小步骤。
  • 现在,如果以数字i结尾的最小数字大于具有单位数字 iN ,则打印“-1” ,因为不可能将数字之和设为N。
  • 否则,答案将是创建一个以数字i结尾的数字的最少步骤,该数字与N的个位数字相同,因为可以通过将这些数字插入任何有助于答案的数字中来获得剩余的数字。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
int minCount(int N, int K)
{
    // Stores the smallest number that
    // ends with digit i (0, 9)
    int SmallestNumber[10];
 
    // Stores the minimum number of
    // steps to create a number ending
    // with digit i
    int MinimumSteps[10];
 
    // Initialize elements as infinity
    for (int i = 0; i <= 9; i++) {
        SmallestNumber[i] = INT_MAX;
        MinimumSteps[i] = INT_MAX;
    }
 
    for (int i = 1; i <= 10; i++) {
 
        int num = K * i;
 
        // Minimum number
        // ending with digit i
        SmallestNumber[num % 10]
            = min(
                SmallestNumber[num % 10],
                num);
 
        // Minimum steps to create a
        // number ending with digit i
        MinimumSteps[num % 10]
            = min(
                MinimumSteps[num % 10], i);
    }
 
    // If N < SmallestNumber then,
    // return -1
    if (N < SmallestNumber[N % 10]) {
        return -1;
    }
 
    // Otherwise, return answer
    else {
        return MinimumSteps[N % 10];
    }
}
 
// Driver Code
int main()
{
    int N = 42, K = 7;
    cout << minCount(N, K);
 
    return 0;
}


Java
// Java program for above approach
import java.util.*;
 
class GFG{
 
static int minCount(int N, int K)
{
     
    // Stores the smallest number that
    // ends with digit i (0, 9)
    int SmallestNumber[] = new int[10];
 
    // Stores the minimum number of
    // steps to create a number ending
    // with digit i
    int MinimumSteps[] = new int[10];
 
    // Initialize elements as infinity
    for(int i = 0; i <= 9; i++)
    {
        SmallestNumber[i] = Integer.MAX_VALUE;
        MinimumSteps[i] = Integer.MAX_VALUE;
    }
 
    for(int i = 1; i <= 10; i++)
    {
        int num = K * i;
         
        // Minimum number
        // ending with digit i
        SmallestNumber[num % 10] = Math.min(
            SmallestNumber[num % 10], num);
 
        // Minimum steps to create a
        // number ending with digit i
        MinimumSteps[num % 10] = Math.min(
            MinimumSteps[num % 10], i);
    }
 
    // If N < SmallestNumber then,
    // return -1
    if (N < SmallestNumber[N % 10])
    {
        return -1;
    }
 
    // Otherwise, return answer
    else
    {
        return MinimumSteps[N % 10];
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 42, K = 7;
   
    System.out.println(minCount(N, K));
}
}
 
// This code is contributed by hritikrommie


Python3
# Python3 program for the above approach
import sys
 
def minCount(N, K):
   
    # Stores the smallest number that
    # ends with digit i (0, 9)
    SmallestNumber = [0 for i in range(10)]
 
    # Stores the minimum number of
    # steps to create a number ending
    # with digit i
    MinimumSteps = [0 for i in range(10)]
 
    # Initialize elements as infinity
    for i in range(10):
        SmallestNumber[i] = sys.maxsize;
        MinimumSteps[i] = sys.maxsize
 
    for i in range(1,11,1):
        num = K * i
 
        # Minimum number
        # ending with digit i
        SmallestNumber[num % 10] = min(SmallestNumber[num % 10],num)
 
        # Minimum steps to create a
        # number ending with digit i
        MinimumSteps[num % 10] = min(MinimumSteps[num % 10], i)
 
    # If N < SmallestNumber then,
    # return -1
    if (N < SmallestNumber[N % 10]):
        return -1
 
    # Otherwise, return answer
    else:
        return MinimumSteps[N % 10]
 
# Driver Code
if __name__ == '__main__':
    N = 42
    K = 7
    print(minCount(N, K))
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
 
public class GFG{
     
    static int minCount(int N, int K)
    {
         
        // Stores the smallest number that
        // ends with digit i (0, 9)
        int[] SmallestNumber = new int[10];
     
        // Stores the minimum number of
        // steps to create a number ending
        // with digit i
        int[] MinimumSteps = new int[10];
     
        // Initialize elements as infinity
        for(int i = 0; i <= 9; i++)
        {
            SmallestNumber[i] = Int32.MaxValue;
            MinimumSteps[i] = Int32.MaxValue;
        }
     
        for(int i = 1; i <= 10; i++)
        {
            int num = K * i;
             
            // Minimum number
            // ending with digit i
            SmallestNumber[num % 10] = Math.Min(
                SmallestNumber[num % 10], num);
     
            // Minimum steps to create a
            // number ending with digit i
            MinimumSteps[num % 10] = Math.Min(
                MinimumSteps[num % 10], i);
        }
     
        // If N < SmallestNumber then,
        // return -1
        if (N < SmallestNumber[N % 10])
        {
            return -1;
        }
     
        // Otherwise, return answer
        else
        {
            return MinimumSteps[N % 10];
        }
    }
     
    // Driver Code
    static public void Main ()
    {
        int N = 42, K = 7;
       
        Console.Write(minCount(N, K));
    }
}
 
// This code is contributed by shubhamsingh10


Javascript


输出:
6

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