📌  相关文章
📜  大于或等于 N 且位数不超过 S 的最小数

📅  最后修改于: 2021-10-26 05:44:49             🧑  作者: Mango

给定整数N和整数S ,任务是找到大于或等于N且其数字之和不超过S的最小数。

例子:

方法:该问题可以使用贪心方法解决。请按照以下步骤解决问题。

  1. 检查N的数字总和是否不超过S ,返回N
  2. 初始化一个变量,比如ans等于给定的整数Nk1来存储10的幂。
  3. 整数范围内最多可以有10位数字。
  4. i = 0迭代到 8 。在每次迭代时,计算最后一位数字为(ans / k)%10
  5. 使最后一位数字为0的总和是k*((10-last_digit)%10) 。将其添加到ans
  6. 检查ans的数字总和。如果不超过S ,则打印ans并中断。否则,更新k作为K = K * 10,并重复上述步骤。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to calculate sum
// digits of n
int sum(int n)
{
    int res = 0;
    while (n > 0) {
        res += n % 10;
        n /= 10;
    }
    return res;
}
 
// Function to find the smallest
// possible integer satisfying the
// given condition
int smallestNumber(int n, int s)
{
    // If the sum of digits
    // is already smaller than S
    if (sum(n) <= s) {
        return n;
    }
 
    // Initialize variables
    int ans = n, k = 1;
 
    for (int i = 0; i < 9; ++i) {
 
        // Finding last kth digit
        int digit = (ans / k) % 10;
 
        // Add remaining to make digit 0
        int add = k * ((10 - digit) % 10);
 
        ans += add;
 
        // If sum of digits
        // does not exceed S
        if (sum(ans) <= s) {
            break;
        }
 
        // Update k
        k *= 10;
    }
    return ans;
}
 
// Driver Code
int main()
{
 
    // Given N and S
    int N = 3, S = 2;
 
    // Function call
    cout << smallestNumber(N, S) << endl;
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to calculate sum
// digits of n
static int sum(int n)
{
    int res = 0;
    while (n > 0)
    {
        res += n % 10;
        n /= 10;
    }
    return res;
}
 
// Function to find the smallest
// possible integer satisfying the
// given condition
static int smallestNumber(int n, int s)
{
     
    // If the sum of digits
    // is already smaller than S
    if (sum(n) <= s)
    {
        return n;
    }
 
    // Initialize variables
    int ans = n, k = 1;
 
    for(int i = 0; i < 9; ++i)
    {
         
        // Finding last kth digit
        int digit = (ans / k) % 10;
 
        // Add remaining to make digit 0
        int add = k * ((10 - digit) % 10);
 
        ans += add;
 
        // If sum of digits
        // does not exceed S
        if (sum(ans) <= s)
        {
            break;
        }
 
        // Update k
        k *= 10;
    }
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given N and S
    int N = 3, S = 2;
 
    // Function call
    System.out.println(smallestNumber(N, S));
}
}
 
// This code is contributed by akhilsaini


Python3
# Python program for the above approach
 
# Function to calculate
# sum of digits of n
def sum(n):
    sm = 0
    while(n > 0):
        sm += n % 10
        n //= 10
    return sm
 
# Function to find the smallest
# possible integer satisfying the
# given condition
def smallestNumber(n, s):
 
# If sum of digits is
# already smaller than s
    if(sum(n) <= s):
        return n
 
# Initialize variables
    ans, k = n, 1
 
    for i in range(9):
 
# Find the k-th digit
        digit = (ans // k) % 10
 
# Add remaining
        add = k * ((10 - digit) % 10)
 
        ans += add
 
# If sum of digits
# does not exceed s
        if(sum(ans) <= s):
            break
 
# Update K
        k *= 10
 
# Return answer
    return ans
 
# Driver Code
 
# Given N and S
n, s = 3, 2
 
# Function call
print(smallestNumber(n, s))


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to calculate sum
// digits of n
static int sum(int n)
{
    int res = 0;
    while (n > 0)
    {
        res += n % 10;
        n /= 10;
    }
    return res;
}
 
// Function to find the smallest
// possible integer satisfying the
// given condition
static int smallestNumber(int n, int s)
{
     
    // If the sum of digits
    // is already smaller than S
    if (sum(n) <= s)
    {
        return n;
    }
 
    // Initialize variables
    int ans = n, k = 1;
 
    for(int i = 0; i < 9; ++i)
    {
         
        // Finding last kth digit
        int digit = (ans / k) % 10;
 
        // Add remaining to make digit 0
        int add = k * ((10 - digit) % 10);
 
        ans += add;
 
        // If sum of digits
        // does not exceed S
        if (sum(ans) <= s)
        {
            break;
        }
         
        // Update k
        k *= 10;
    }
    return ans;
}
 
// Driver Code
public static void Main()
{
     
    // Given N and S
    int N = 3, S = 2;
 
    // Function call
    Console.WriteLine(smallestNumber(N, S));
}
}
 
// This code is contributed by akhilsaini


Javascript


输出:
10

时间复杂度: O(log 2 10 (N)) 其中 N 是给定的整数。
空间复杂度: O(1)