📜  数字计数,使得数字与数字总和之差不小于L

📅  最后修改于: 2021-05-31 19:53:39             🧑  作者: Mango

给定自然数N和整数L ,任务是查找小于或等于N的数字计数,以使数字与其数字总和之间的差不小于L。

例子:

Input: N = 1500, L = 30
Output: 1461

Input: N = 1546300, L = 30651
Output: 1515631

方法:
我们的解决方案基于一个简单的观察,即如果一个数字(例如X)使得X与sumOfDigits(X)之差小于或等于L,那么X + 1也是一个有效数字。因此,我们必须使用二进制搜索找到最小的X。

执行:

C++
// C++ implementation of above approach
#include 
using namespace std;
  
// Function to calculate the
// sum of digits
int sumOfDigits(long long n)
{
    long long sum = 0;
  
    while (n > 0) {
        sum += n % 10;
        n /= 10;
    }
  
    return sum;
}
  
// Function to count the numbers
long long countDigits(long long num, long long limit)
{
    long long left = 1, right = num, result = 0;
  
    // use binary search
    while (left <= right) {
  
        long long mid = (right + left) / 2;
  
        // Check if you are at a valid number or not
        if ((mid - sumOfDigits(mid)) >= limit) {
  
            // update the answer at each valid step
            result = num - mid + 1;
            right = mid - 1;
        }
        else {
  
            left = mid + 1;
        }
    }
  
    // return the answer
    return result;
}
  
// Driver code
int main()
{
    // Get the value of N and L
    long long N = 1546300, L = 30651;
  
    // Count the numbers
    cout << countDigits(N, L);
  
    return 0;
}


Java
// Java implementation of above approach
  
class GFG
{
    // Function to calculate the
    // sum of digits
    static long sumOfDigits( long n )
    {
        long sum = 0;
      
        while (n > 0) 
        {
            sum += n % 10;
            n /= 10;
        }
      
        return sum;
    }
      
    // Function to count the numbers
    static long countDigits(long num, long limit)
    {
        long left = 1, right = num, result = 0;
      
        // use binary search
        while (left <= right)
        {
            long mid = (right + left) / 2;
      
            // Check if you are at a valid number or not
            if ((mid - sumOfDigits(mid)) >= limit)
            {
      
                // update the answer at each valid step
                result = num - mid + 1;
                right = mid - 1;
            }
            else
            {
                left = mid + 1;
            }
        }
      
        // return the answer
        return result;
    }
      
    // Driver code
    public static void main(String []args)
    {
        // Get the value of N and L
        long N = 1546300, L = 30651;
      
        // Count the numbers
        System.out.println(countDigits(N, L));
    }
}
  
// This code is contributed by ihritik


Python3
# Python3 program for above approach
  
# Function to calculate the sum of digits
def sumOfDigits(n):
    sum = 0
  
    while (n > 0):
        sum += n % 10
        n = int(n / 10)
  
    return sum
  
# Function to count the numbers
def countDigits(num, limit):
    left = 1
    right = num
    result = 0
  
    # use binary search
    while (left <= right):
        mid = int((right + left) / 2)
  
        # Check if you are at a valid number or not
        if ((mid - sumOfDigits(mid)) >= limit):
              
            # update the answer at each valid step
            result = num - mid + 1
            right = mid - 1
          
        else:
            left = mid + 1
          
    # return the answer
    return result
  
# Driver code
if __name__ == '__main__':
      
    # Get the value of N and L
    N = 1546300
    L = 30651
  
    # Count the numbers
    print(countDigits(N, L))
  
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of above approach
using System;
  
class GFG
{
    // Function to calculate the
    // sum of digits
    static long sumOfDigits( long n )
    {
        long sum = 0;
      
        while (n > 0) 
        {
            sum += n % 10;
            n /= 10;
        }
      
        return sum;
    }
      
    // Function to count the numbers
    static long countDigits(long num, long limit)
    {
        long left = 1, right = num, result = 0;
      
        // use binary search
        while (left <= right) {
      
            long mid = (right + left) / 2;
      
            // Check if you are at a valid number or not
            if ((mid - sumOfDigits(mid)) >= limit) 
            {
      
                // update the answer at each valid step
                result = num - mid + 1;
                right = mid - 1;
            }
            else 
            {
      
                left = mid + 1;
            }
        }
      
        // return the answer
        return result;
    }
      
    // Driver code
    public static void Main()
    {
        // Get the value of N and L
        long N = 1546300, L = 30651;
      
        // Count the numbers
        Console.WriteLine(countDigits(N, L));
    }
}
  
// This code is contributed by ihritik


PHP
 0)
    { 
        $sum += $n % 10; 
        $n /= 10; 
    } 
  
    return $sum; 
} 
  
// Function to count the numbers 
function countDigits($num, $limit) 
{ 
    $left = 1;
    $right = $num;
    $result = 0; 
  
    // use binary search 
    while ($left <= $right) 
    { 
  
        $mid = floor(($right + $left) / 2); 
  
        // Check if you are at a valid number or not 
        if (($mid - sumOfDigits($mid)) >= $limit) 
        { 
  
            // update the answer at each valid step 
            $result = $num - $mid + 1; 
            $right = $mid - 1; 
        } 
        else 
        { 
  
            $left = $mid + 1; 
        } 
    } 
  
    // return the answer 
    return $result; 
} 
  
// Driver code 
  
// Get the value of N and L 
$N = 1546300;
$L = 30651; 
  
// Count the numbers 
echo countDigits($N, $L); 
  
// This code is contributed by Ryuga
?>


输出:
1515631

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

想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”