给定两个整数N和K ,任务是找到可以加到N的最小整数X ,以使新形成的数字的总和不超过K。
例子:
Input: N = 1, K = 1
Output: 0
Explanation:
The sum of the digits of the given number is 1, which is already equal to K(=1).
Input: N = 11, K = 1
Output: 89
Explanation:
Adding the number 89 to the given number 11 results to 100.
The sum of digits of the new number formed is 1 which does not exceed K(=1).
Therefore, the minimum number that can be added is 89.
方法:请按照以下步骤解决问题:
- 检查给定数字N的数字总和是否不超过K。如果发现为真,则添加的最小数字为0。
- 现在,从单位位置开始计算数字总和,直到数字总和超过K为止。
- 现在,找到数字总和大于或等于K的N部分。因此,请除去该部分的最后一位,以使这些数字的总和小于K。
- 现在,将1加到新获得的数字上,因为它将使数字的总和小于或等于K。
- 现在,要获得超过N且位数小于或等于K的新数字,请将数字乘以10 P +1 ,其中P是总和不超过K的位数。
- 现在从新数字中减去N以获得结果X。
- 完成上述步骤后,打印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)