需要添加到 M 以使其可被 N 整除的最小数
给定两个正整数M和N ,任务是计算需要添加到M的最小数字,以使其可被N整除。
例子:
Input: M = 6, N = 7
Output: 1
Explanation: 1 is the smallest number that can be added to 6 to make it divisible by 7.
Input: M = 100, N = 28
Output: 12
方法:这个想法是找到大于或等于M的最小数字,即可以被N整除,然后从中减去M。要获得N ≥ M的最小倍数,请将M + N除以N 。如果余数为0 ,则值为M 。否则,值为M + N – 余数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the smallest
// number greater than or equal
// to N, that is divisible by k
int findNum(int N, int K)
{
int rem = (N + K) % K;
if (rem == 0)
return N;
else
return N + K - rem;
}
// Function to find the smallest
// number required to be added to
// to M to make it divisible by N
int findSmallest(int M, int N)
{
// Stores the smallest multiple
// of N, greater than or equal to M
int x = findNum(M, N);
// Return the result
return x - M;
}
// Driver Code
int main()
{
// Given Input
int M = 100, N = 28;
// Function Call
cout << findSmallest(M, N);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to find the smallest
// number greater than or equal
// to N, that is divisible by k
public static int findNum(int N, int K)
{
int rem = (N + K) % K;
if (rem == 0)
return N;
else
return N + K - rem;
}
// Function to find the smallest
// number required to be added to
// to M to make it divisible by N
public static int findSmallest(int M, int N)
{
// Stores the smallest multiple
// of N, greater than or equal to M
int x = findNum(M, N);
// Return the result
return x - M;
}
// Driver Code
public static void main(String args[])
{
// Given Input
int M = 100, N = 28;
// Function Call
System.out.println(findSmallest(M, N));
}}
// This code is contributed by SoumikMondal
Python3
# Python3 program for the above approach
# Function to find the smallest
# number greater than or equal
# to N, that is divisible by k
def findNum(N, K):
rem = (N + K) % K
if (rem == 0):
return N
else:
return N + K - rem
# Function to find the smallest
# number required to be added to
# to M to make it divisible by N
def findSmallest(M, N):
# Stores the smallest multiple
# of N, greater than or equal to M
x = findNum(M, N)
# Return the result
return x - M
# Driver Code
if __name__ == '__main__':
# Given Input
M = 100
N = 28
# Function Call
print(findSmallest(M, N))
# This code is contributed by ipg2016107
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the smallest
// number greater than or equal
// to N, that is divisible by k
public static int findNum(int N, int K)
{
int rem = (N + K) % K;
if (rem == 0)
return N;
else
return N + K - rem;
}
// Function to find the smallest
// number required to be added to
// to M to make it divisible by N
public static int findSmallest(int M, int N)
{
// Stores the smallest multiple
// of N, greater than or equal to M
int x = findNum(M, N);
// Return the result
return x - M;
}
// Driver Code
public static void Main()
{
// Given Input
int M = 100, N = 28;
// Function Call
Console.WriteLine(findSmallest(M, N));
}
}
// This code is contributed by susmitakundugoaldanga
Javascript
输出:
12
时间复杂度: O(1)
辅助空间: O(1)