📜  可以加到N的X的最小值,以将数字的总和最小化到≤K

📅  最后修改于: 2021-05-07 08:29:02             🧑  作者: Mango

给定两个整数NK ,任务是找到可以加到N的最小整数X ,以使新形成的数字的总和不超过K。

例子:

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

  1. 检查给定数字N的数字总和是否不超过K。如果发现为真,则添加的最小数字为0。
  2. 现在,从单位位置开始计算数字总和,直到数字总和超过K为止。
  3. 现在,找到数字总和大于或等于KN部分。因此,请除去该部分的最后一位,以使这些数字的总和小于K。
  4. 现在,将1加到新获得的数字上,因为它将使数字的总和小于或等于K。
  5. 现在,要获得超过N且位数小于或等于K的新数字,请将数字乘以10 P +1 ,其中P是总和不超过K的位数。
  6. 现在从新数字中减去N以获得结果X。
  7. 完成上述步骤后,打印X的值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum number
// needed to be added so that the sum
// of the digits does not exceed K
int minDigits(int N, int K)
{
    // Find the number of digits
    int digits_num
        = floor(log10(N) + 1);
 
    int temp_sum = 0;
    int temp = digits_num;
    int result;
 
    int X, var;
 
    int sum = 0;
    int num2 = N;
 
    // Calculate sum of the digits
    while (num2 != 0) {
 
        // Add the digits of num2
        sum += num2 % 10;
        num2 /= 10;
    }
 
    // If the sum of the digits of N
    // is less than or equal to K
    if (sum <= K) {
 
        // No number needs to
        // be added
        X = 0;
    }
 
    // Otherwise
    else {
 
        while (temp > 0) {
 
            // Calculate the sum of digits
            // from least significant digit
            var = (N / (pow(10, temp - 1)));
            temp_sum += var % 10;
 
            // If sum exceeds K
            if (temp_sum >= K) {
 
                // Increase previous
                // digit by 1
                var /= 10;
                var++;
 
                // Add zeros to the end
                result
                    = var * pow(10, temp);
 
                break;
            }
 
            temp--;
        }
 
        // Calculate difference
        // between the result and N
        X = result - N;
 
        // Return the result
        return X;
    }
}
 
// Driver Code
int main()
{
    int N = 11, K = 1;
    cout << minDigits(N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
import java.io.*;
 
class GFG{
     
// Function to find the minimum number
// needed to be added so that the sum
// of the digits does not exceed K
static int minDigits(int N, int K)
{
     
    // Find the number of digits
    int digits_num = (int)Math.floor(
                          Math.log(N) + 1);
   
    int temp_sum = 0;
    int temp = digits_num;
    int result = 0;
    int X, var;
    int sum = 0;
    int num2 = N;
   
    // Calculate sum of the digits
    while (num2 != 0)
    {
         
        // Add the digits of num2
        sum += num2 % 10;
        num2 /= 10;
    }
   
    // If the sum of the digits of N
    // is less than or equal to K
    if (sum <= K)
    {
         
        // No number needs to
        // be added
        X = 0;
    }
   
    // Otherwise
    else
    {
        while (temp > 0)
        {
             
            // Calculate the sum of digits
            // from least significant digit
            var = (N / ((int)Math.pow(
                   10, temp - 1)));
            temp_sum += var % 10;
   
            // If sum exceeds K
            if (temp_sum >= K)
            {
                 
                // Increase previous
                // digit by 1
                var /= 10;
                var++;
   
                // Add zeros to the end
                result = var * (int)Math.pow(
                         10, temp);
                break;
            }
            temp--;
        }
         
        // Calculate difference
        // between the result and N
        X = result - N;
   
        // Return the result
        return X;
    }
    return -1;
}
 
// Driver Code
public static void main(String args[])
{
    int N = 11;
    int K = 1;
        
    System.out.println(minDigits(N, K));
}
}
 
// This code is contributed by bikram2001jha


Python3
# Python program for
# the above approach
import math;
 
# Function to find the minimum number
# needed to be added so that the sum
# of the digits does not exceed K
def minDigits(N, K):
 
    # Find the number of digits
    digits_num = int(math.floor(math.log(N) + 1));
 
    temp_sum = 0;
    temp = digits_num;
    result = 0;
    X = 0; var = 0;
    sum1 = 0;
    num2 = N;
 
    # Calculate sum of the digits
    while (num2 != 0):
 
        # Add the digits of num2
        sum1 += num2 % 10;
        num2 /= 10;   
 
    # If the sum of the digits of N
    # is less than or equal to K
    if (sum1 <= K):
 
        # No number needs to
        # be added
        X = 0;   
 
    # Otherwise
    else:
        while (temp > 0):
 
            # Calculate the sum of digits
            # from least significant digit
            var = int(N // (pow(10, temp - 1)));
            temp_sum += var % 10;
 
            # If sum exceeds K
            if (temp_sum >= K):
 
                # Increase previous
                # digit by 1
                var = var // 10;
                var += 1;
 
                # Add zeros to the end
                result = var * int(pow(10, temp));
                break;
             
            temp -= 1;       
 
        # Calculate difference
        # between the result and N
        X = result - N;
 
        # Return the result
        return X;
     
    return -1;
 
# Driver Code
if __name__ == '__main__':
    N = 11;
    K = 1;
    print(minDigits(N, K));
 
# This code is contributed by 29AjayKumar


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the minimum number
// needed to be added so that the sum
// of the digits does not exceed K
static int minDigits(int N, int K)
{
     
    // Find the number of digits
    int digits_num = (int)Math.Floor(
                          Math.Log(N) + 1);
   
    int temp_sum = 0;
    int temp = digits_num;
    int result = 0;
    int X, var;
    int sum = 0;
    int num2 = N;
   
    // Calculate sum of the digits
    while (num2 != 0)
    {
         
        // Add the digits of num2
        sum += num2 % 10;
        num2 /= 10;
    }
   
    // If the sum of the digits of N
    // is less than or equal to K
    if (sum <= K)
    {
         
        // No number needs to
        // be added
        X = 0;
    }
   
    // Otherwise
    else
    {
        while (temp > 0)
        {
             
            // Calculate the sum of digits
            // from least significant digit
            var = (N / ((int)Math.Pow(
                   10, temp - 1)));
                    
            temp_sum += var % 10;
   
            // If sum exceeds K
            if (temp_sum >= K)
            {
                 
                // Increase previous
                // digit by 1
                var /= 10;
                var++;
   
                // Add zeros to the end
                result = var * (int)Math.Pow(
                         10, temp);
                break;
            }
            temp--;
        }
         
        // Calculate difference
        // between the result and N
        X = result - N;
   
        // Return the result
        return X;
    }
    return -1;
}
 
// Driver Code
public static void Main(String []args)
{
    int N = 11;
    int K = 1;
        
    Console.WriteLine(minDigits(N, K));
}
}
 
// This code is contributed by Amit Katiyar


输出:
89






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