📜  最小整数,其数字和为M且为N的倍数

📅  最后修改于: 2021-06-26 01:00:18             🧑  作者: Mango

给定两个正整数N和M,任务是找到可被N整除且数字总和为M的最小正整数。如果int范围内不存在这样的整数,则打印-1
例子:

Input: N = 13, M = 32
Output: 8879
8879 is divisible by 13 and its 
Sum of digits of 8879 is 8+8+7+9 = 32 
i.e. equals to M

Input: N = 8, M = 32;
Output: 8888

方法:从N开始,遍历n的所有倍数,然后检查其数字总和是否等于m。此问题可以通过log n (INT_MAX)* log 10 (INT_MAX)时间解决。通过以至少m / 9位数的数字开始迭代,可以提高此方法的效率。
下面是该方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Function to return digit sum
int digitSum(int n)
{
    int ans = 0;
    while (n) {
        ans += n % 10;
        n /= 10;
    }
 
    return ans;
}
 
// Function to find out the smallest integer
int findInt(int n, int m)
{
    int minDigit = floor(m / 9);
 
    // Start of the iterator (Smallest multiple of n)
    int start = pow(10, minDigit) -
                (int)pow(10, minDigit) % n;
 
    while (start < INT_MAX) {
        if (digitSum(start) == m)
            return start;
        else
            start += n;
    }
    return -1;
}
 
// Driver code
int main()
{
    int n = 13, m = 32;
    cout << findInt(n, m);
    return 0;
}


Java
// Java implementation of the above approach
 
class GFG
{
 
    // Function to return digit sum
    static int digitSum(int n)
    {
        int ans = 0;
        while (n != 0)
        {
            ans += n % 10;
            n /= 10;
        }
     
        return ans;
    }
     
    // Function to find out the
    // smallest integer
    static int findInt(int n, int m)
    {
        int minDigit = (int)Math.floor((double)(m / 9));
     
        // Start of the iterator (Smallest multiple of n)
        int start = (int)Math.pow(10, minDigit) -
                    (int)Math.pow(10, minDigit) % n;
     
        while (start < Integer.MAX_VALUE)
        {
            if (digitSum(start) == m)
                return start;
            else
                start += n;
        }
        return -1;
    }
 
    // Driver code
    static public void main(String args[])
    {
        int n = 13, m = 32;
        System.out.print(findInt(n, m));
    }
}
 
// This code is contributed
// by Akanksha Rai


Python3
# Python 3 implementation of the
# above approach
from math import floor, pow
 
import sys
 
# Function to return digit sum
def digitSum(n):
    ans = 0;
    while (n):
        ans += n % 10;
        n = int(n / 10);
 
    return ans
 
# Function to find out the smallest
# integer
def findInt(n, m):
    minDigit = floor(m / 9)
 
    # Start of the iterator (Smallest
    # multiple of n)
    start = (int(pow(10, minDigit)) -
             int(pow(10, minDigit)) % n)
 
    while (start < sys.maxsize):
        if (digitSum(start) == m):
            return start
        else:
            start += n
    return -1
 
# Driver code
if __name__ == '__main__':
    n = 13
    m = 32
    print(findInt(n, m))
 
# This code is cotributed by
# Surendra_Gangwar


C#
// C# implementation of the above approach
using System;
 
class GFG
{
 
    // Function to return digit sum
    static int digitSum(int n)
    {
        int ans = 0;
        while (n != 0)
        {
            ans += n % 10;
            n /= 10;
        }
     
        return ans;
    }
     
    // Function to find out the
    // smallest integer
    static int findInt(int n, int m)
    {
        int minDigit = (int)Math.Floor((double)(m / 9));
     
        // Start of the iterator (Smallest multiple of n)
        int start = (int)Math.Pow(10, minDigit) -
                    (int)Math.Pow(10, minDigit) % n;
     
        while (start < int.MaxValue)
        {
            if (digitSum(start) == m)
                return start;
            else
                start += n;
        }
        return -1;
    }
 
    // Driver code
    static public void Main()
    {
        int n = 13, m = 32;
        Console.WriteLine(findInt(n, m));
    }
}
 
// This code is contributed by Ryuga


PHP


Javascript


输出:
8879

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。