算法題:求大于或等于X的最小数字,其位数之和可被Y整除

给定两个整数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/

查看以下更多算法相关的内容:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值