📌  相关文章
📜  大于或等于X的最小数字,其位数之和可被Y整除

📅  最后修改于: 2021-04-27 19:14:14             🧑  作者: Mango

给定两个整数XY ,任务是找到大于或等于X的最小数字,其位数之和可被Y整除。
注意: 1 <= X <= 1000,1 <= Y <= 50
例子:

方法:这个问题的想法是从X运行一个循环,并检查每个整数的位数之和是否可被Y整除。返回其位数之和可被Y整除的第一个数字。给定XY的约束,答案总是存在的。
下面是上述方法的实现:

C++
// C++ program to find the smallest number
// greater than or equal to X and divisible by Y
 
#include 
using namespace std;
 
#define MAXN 10000000
 
// Function that returns the sum
// of digits of a number
int sumOfDigits(int n)
{
    // Initialize variable to
    // store the sum
    int sum = 0;
 
    while (n > 0) {
 
        // Add the last digit
        // of the number
        sum += n % 10;
 
        // Remove the last digit
        // from the number
        n /= 10;
    }
    return sum;
}
 
// Function that returns the smallest number
// greater than or equal to X and divisible by Y
int smallestNum(int X, int Y)
{
    // Initialize result variable
    int res = -1;
 
    // Loop through numbers greater
    // than  equal to X
    for (int i = X; i < MAXN; i++) {
 
        // Calculate sum of digits
        int sum_of_digit = sumOfDigits(i);
 
        // Check if sum of digits
        // is divisible by Y
        if (sum_of_digit % Y == 0) {
            res = i;
            break;
        }
    }
 
    return res;
}
 
// Driver code
int main()
{
    int X = 5923, Y = 13;
 
    cout << smallestNum(X, Y);
 
    return 0;
}


Java
// Java program to find the smallest number
// greater than or equal to X and divisible by Y
 
class GFG{
 
static final int MAXN = 10000000;
 
// Function that returns the sum
// of digits of a number
static int sumOfDigits(int n)
{
     
    // Initialize variable to
    // store the sum
    int sum = 0;
    while (n > 0)
    {
 
         // Add the last digit
         // of the number
         sum += n % 10;
 
         // Remove the last digit
         // from the number
         n /= 10;
    }
    return sum;
}
 
// Function that returns the smallest number
// greater than or equal to X and divisible by Y
static int smallestNum(int X, int Y)
{
     
    // Initialize result variable
    int res = -1;
 
    // Loop through numbers greater
    // than equal to X
    for (int i = X; i < MAXN; i++)
    {
 
        // Calculate sum of digits
        int sum_of_digit = sumOfDigits(i);
 
        // Check if sum of digits
        // is divisible by Y
        if (sum_of_digit % Y == 0)
        {
            res = i;
            break;
        }
    }
    return res;
}
 
// Driver code
public static void main(String[] args)
{
    int X = 5923, Y = 13;
    System.out.print(smallestNum(X, Y));
}
}
 
// This code is contributed by Rohit_ranjan


Python3
# Python3 program to find the smallest number
# greater than or equal to X and divisible by Y
 
MAXN = 10000000
 
# Function that returns the 
# sum of digits of a number
def sumOfDigits(n):
     
    # Initialize variable 
    # to store the sum
    sum = 0
     
    while(n > 0):
         
        # Add the last digit
        # of the number
        sum += n % 10
         
        # Remove the last digit
        # from the number
        n //= 10
         
    return sum
 
# Function that returns the smallest number
# greater than or equal to X and divisible by Y
def smallestNum(X, Y):
     
    # Initialize result variable
    res = -1;
 
    # Loop through numbers greater
    # than equal to X
    for i in range(X, MAXN):
         
        # Calculate sum of digits
        sum_of_digit = sumOfDigits(i)
         
        # Check if sum of digits
        # is divisible by Y
        if sum_of_digit % Y == 0:
            res = i
            break
     
    return res    
             
# Driver code
if __name__=='__main__':
     
    (X, Y) = (5923, 13)
      
    print(smallestNum(X, Y))
 
# This code is contributed by rutvik_56


C#
// C# program to find the smallest number
// greater than or equal to X and divisible by Y
using System;
 
class GFG{
 
static readonly int MAXN = 10000000;
 
// Function that returns the sum
// of digits of a number
static int sumOfDigits(int n)
{
     
    // Initialize variable to
    // store the sum
    int sum = 0;
    while(n > 0)
    {
         
         // Add the last digit
         // of the number
         sum += n % 10;
 
         // Remove the last digit
         // from the number
         n /= 10;
    }
    return sum;
}
 
// Function that returns the smallest number
// greater than or equal to X and divisible by Y
static int smallestNum(int X, int Y)
{
     
    // Initialize result variable
    int res = -1;
 
    // Loop through numbers greater
    // than equal to X
    for(int i = X; i < MAXN; i++)
    {
     
        // Calculate sum of digits
        int sum_of_digit = sumOfDigits(i);
 
        // Check if sum of digits
        // is divisible by Y
        if(sum_of_digit % Y == 0)
        {
           res = i;
           break;
        }
    }
    return res;
}
 
// Driver code
public static void Main(String[] args)
{
    int X = 5923, Y = 13;
    Console.Write(smallestNum(X, Y));
}
}
 
// This code is contributed by gauravrajput1


Javascript


输出:
5939