📌  相关文章
📜  最小化增量以使 N 的数字总和最多为 S

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

最小化增量以使 N 的数字总和最多为 S

给定两个正整数NS 。任务是最小化N (N = N + 1)上的增量数,以使N的数字总和小于或等于S
注意: N最多可以是18位数字和1 <= S <= 162

例子:

方法:这里的关键观察是,为了以最小增量最小化任何数字的数字总和,需要最小化从单位开始的数字。请按照以下步骤解决给定的问题。

  • 对于基本情况,如果N的数字总和已经小于或等于S ,则输出为0
  • 初始化变量,比如ans = 0 和p = 1,其中ans将存储所需的最小增量, p将跟踪数字位置,从单位位置开始。
  • 循环遍历N可以拥有的最大位数(即 18 位)并找到N的最后一位。让它用数字表示。然后找到将此数字转换为0所需的最小增量。表示为 说,
    • 数字 = (N / p) % 10
    • 添加 = p * (10 – 数字)
    • 通过add增加Nans
    • 现在检查N的数字总和是否小于或等于S
      • 如果条件为真,则退出循环并输出答案。
      • 否则,将p乘以 10 以访问N的单位位置的倒数第二位。
  • 循环再次运行并执行相同的步骤,直到找到所需的答案。
  • 返回ans作为最终答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the sum
// of digits of N
int findSum(long long int N)
{
    // Stores the sum of digits of N
    int res = 0;
 
    // Loop to extract the digits of N
    // and find their sum
    while (N) {
 
        // Extracting the last digit of N
        // and adding it to res
        res += (N % 10);
 
        // Update N
        N /= 10;
    }
 
    return res;
}
 
// Function to find the minimum increments
// required to make the sum of digits of N
// less than or equal to S.
long long int minIncrements(long long int N, int S)
{
    // If the sum of digits of N is less than
    // or equal to S
    if (findSum(N) <= S) {
 
        // Output 0
        return 0;
    }
 
    // variable to access the digits of N
    long long int p = 1;
 
    // Stores the required answer
    long long int ans = 0;
 
    // Loop to access the digits of N
    for (int i = 0; i <= 18; ++i) {
 
        // Stores the digit of N starting
        // from unit's place
        int digit = (N / p) % 10;
 
        // Stores the increment required
        // to make the digit 0
        long long int add = p * (10 - digit);
 
        // Update N
        N += add;
 
        // Update ans
        ans += add;
 
        // If the sum of digits of N is less than
        // or equal to S
        if (findSum(N) <= S) {
 
            // Break from the loop
            break;
        }
 
        // Update p to access the next digit
        p = p * 10;
    }
 
    return ans;
}
 
// Driver Code
int main()
{
 
    // Given N and S
    long long int N = 345899211156769;
    int S = 20;
 
    // Function call
    cout << minIncrements(N, S);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
public class GFG
{
   
// Function to find the sum
// of digits of N
static int findSum(long N)
{
   
    // Stores the sum of digits of N
    int res = 0;
 
    // Loop to extract the digits of N
    // and find their sum
    while (N != 0) {
 
        // Extracting the last digit of N
        // and adding it to res
        res += (N % 10);
 
        // Update N
        N /= 10;
    }
 
    return res;
}
 
// Function to find the minimum increments
// required to make the sum of digits of N
// less than or equal to S.
static long minIncrements(long N, int S)
{
   
    // If the sum of digits of N is less than
    // or equal to S
    if (findSum(N) <= S) {
 
        // Output 0
        return 0;
    }
 
    // variable to access the digits of N
    long p = 1;
 
    // Stores the required answer
    long ans = 0;
 
    // Loop to access the digits of N
    for (int i = 0; i <= 18; ++i) {
 
        // Stores the digit of N starting
        // from unit's place
        long digit = (N / p) % 10;
 
        // Stores the increment required
        // to make the digit 0
        long add = p * (10 - digit);
 
        // Update N
        N += add;
 
        // Update ans
        ans += add;
 
        // If the sum of digits of N is less than
        // or equal to S
        if (findSum(N) <= S) {
 
            // Break from the loop
            break;
        }
 
        // Update p to access the next digit
        p = p * 10;
    }
 
    return ans;
}
 
// Driver Code
public static void main(String args[])
{
    // Given N and S
    long N = 345899211156769L;
    int S = 20;
 
    // Function call
    System.out.println(minIncrements(N, S));
}
}
 
// This code is contributed by Samim Hossain Mondal


Python3
# Python program for the above approach
 
# Function to find the sum
# of digits of N
def findSum(N):
   
  # Stores the sum of digits of N
  res = 0;
 
  # Loop to extract the digits of N
  # and find their sum
  while (N):
 
    # Extracting the last digit of N
    # and adding it to res
    res += (N % 10);
 
    # Update N
    N = N // 10;
   
  return res;
 
# Function to find the minimum increments
# required to make the sum of digits of N
# less than or equal to S.
def minIncrements(N, S):
   
  # If the sum of digits of N is less than
  # or equal to S
  if (findSum(N) <= S):
 
    # Output 0
    return 0;
   
  # variable to access the digits of N
  p = 1;
 
  # Stores the required answer
  ans = 0;
 
  # Loop to access the digits of N
  for i in range(0, 18):
 
    # Stores the digit of N starting
    # from unit's place
    digit = (N // p) % 10;
 
    # Stores the increment required
    # to make the digit 0
    add = p * (10 - digit);
 
    # Update N
    N += add;
 
    # Update ans
    ans += add;
 
    # If the sum of digits of N is less than
    # or equal to S
    if (findSum(N) <= S):
 
      # Break from the loop
      break;
     
    # Update p to access the next digit
    p = p * 10;
   
  return ans;
 
# Driver Code
 
# Given N and S
N = 345899211156769;
S = 20;
 
# Function call
print(minIncrements(N, S))
 
# This code is contributed by saurabh_jaiswal.


C#
// C# program for the above approach
using System;
using System.Collections;
 
class GFG
{
   
// Function to find the sum
// of digits of N
static long findSum(long N)
{
   
    // Stores the sum of digits of N
    long res = 0;
 
    // Loop to extract the digits of N
    // and find their sum
    while (N != 0) {
 
        // Extracting the last digit of N
        // and adding it to res
        res += (N % 10);
 
        // Update N
        N /= 10;
    }
 
    return res;
}
 
// Function to find the minimum increments
// required to make the sum of digits of N
// less than or equal to S.
static long minIncrements(long N, long S)
{
   
    // If the sum of digits of N is less than
    // or equal to S
    if (findSum(N) <= S) {
 
        // Output 0
        return 0;
    }
 
    // variable to access the digits of N
    long p = 1;
 
    // Stores the required answer
    long ans = 0;
 
    // Loop to access the digits of N
    for (int i = 0; i <= 18; ++i) {
 
        // Stores the digit of N starting
        // from unit's place
        long digit = (N / p) % 10;
 
        // Stores the increment required
        // to make the digit 0
        long add = p * (10 - digit);
 
        // Update N
        N += add;
 
        // Update ans
        ans += add;
 
        // If the sum of digits of N is less than
        // or equal to S
        if (findSum(N) <= S) {
 
            // Break from the loop
            break;
        }
 
        // Update p to access the next digit
        p = p * 10;
    }
     
    return ans;
}
 
// Driver Code
public static void Main()
{
    // Given N and S
    long N = 345899211156769;
    long S = 20;
 
    // Function call
    Console.Write(minIncrements(N, S));
}
}
 
// This code is contributed by Samim Hossain Mondal


Javascript


输出
100788843231

时间复杂度: O(18*logN)。

辅助空间: O(1)。