📌  相关文章
📜  可以被C整除且不在[A,B]范围内的最小正整数

📅  最后修改于: 2021-04-23 09:14:22             🧑  作者: Mango

给定三个正整数ABC。任务是找到最小整数X> 0,这样:

  1. X%C = 0
  2. X不能属于[A,B]范围

例子:

方法:

  • 如果C不属于[A,B],C C> B,C是必需的数字。
  • 否则, C的第一个倍数大于B ,这是必需的答案。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the required number
int getMinNum(int a, int b, int c)
{
  
    // If doesn't belong to the range
    // then c is the required number
    if (c < a || c > b)
        return c;
  
    // Else get the next multiple of c
    // starting from b + 1
    int x = ((b / c) * c) + c;
  
    return x;
}
  
// Driver code
int main()
{
    int a = 2, b = 4, c = 4;
    cout << getMinNum(a, b, c);
    return 0;
}


Java
// Java implementation of the approach
import java.io.*; 
import java.math.*; 
public class GFG 
{ 
    // Function to return the required number
    int getMinNum(int a, int b, int c)
    {
  
        // If doesn't belong to the range
        // then c is the required number
        if (c < a || c > b)
        {
            return c;
        }
  
        // Else get the next multiple of c
        // starting from b + 1
        int x = ((b / c) * c) + c;
  
        return x;
    }
  
// Driver code
public static void main(String args[])
{ 
    int a = 2;
    int b = 4;
    int c = 4;
    GFG g = new GFG();
    System.out.println(g.getMinNum(a, b, c)); 
} 
}
  
// This code is contributed by Shivi_Aggarwal


Python3
# Python3 implementation of the approach
# Function to return the required number
def getMinNum(a, b, c):
  
    # If doesn't belong to the range
    # then c is the required number
    if (c < a or c > b):
        return c
  
    # Else get the next multiple of c
    # starting from b + 1
    x = ((b // c) * c) + c
  
    return x
  
# Driver code
a, b, c = 2, 4, 4
print(getMinNum(a, b, c))
  
# This code is contributed by
# Mohit kumar 29


C#
// C# implementation of the approach
using System;
  
class GFG
{
    // Function to return the required number
    static int getMinNum(int a, int b, int c)
    {
  
        // If doesn't belong to the range
        // then c is the required number
        if (c < a || c > b)
        {
            return c;
        }
  
        // Else get the next multiple of c
        // starting from b + 1
        int x = ((b / c) * c) + c;
  
        return x;
    }
  
    // Driver code
    static public void Main ()
    {
        int a = 2, b = 4, c = 4;
        Console.WriteLine( getMinNum(a, b, c));
    }
}
  
// This Code is contributed by ajit..


PHP
 $b) 
        return $c; 
  
    // Else get the next multiple of c 
    // starting from b + 1 
    $x = (floor(($b / $c)) * $c) + $c; 
  
    return $x; 
} 
  
// Driver code 
$a = 2;
$b = 4;
$c = 4; 
  
echo getMinNum($a, $b, $c); 
  
// This code is contributed by Ryuga
?>


输出:
8