给定两个整数X和Y, 任务是找到大于或等于的最小数X其数字总和可被除以Y.
注意: 1<=X<=1000, 1 <=Y<=50.
例子:
输入:X = 10, Y = 5
输出:14
说明:14是大于10的最小数字, 其数字总和(1 + 4 = 5)可被5整除。
输入:X = 5923, Y = 13
输出:5939
方法:这个问题的想法是从X并检查每个整数是否可以被其整数除Y或不。返回其位数之和可被整除的第一个数字Y。给定约束X和Y, 答案始终存在。
下面是上述方法的实现:
C ++
// C++ program to find the smallest number
// greater than or equal to X and divisible by Y
#include <bits/stdc++.h>
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
输出如下:
5939
更多数据结构和算法相关内容请参考:lsbin - IT开发技术:https://www.lsbin.com/
查看以下更多算法相关的内容: