📌  相关文章
📜  最小的数字可被n整除,并且至少有k个尾随零

📅  最后修改于: 2021-04-24 15:15:47             🧑  作者: Mango

给出了两个整数n和k。我们的任务是打印n的K舍入。 K舍入是最小的正整数X,因此x以k或多个零结尾并且可以被n整除。
例子 :

Input :  n = 30, k = 3.
Output : 3000
3000 is the smallest number that
has at-least k 0s and is divisible
by n.

Input : n = 375, k = 4.
Output : 30000

方法1:
蛮力方法是从result = 10 k开始。检查结果是否除以n。如果是,那就是答案,否则将其增加10 k
方法2:有效的方法是计算10 k和n的LCM。
假设n = 375,k = 4。
结果= 10000。
现在,375和10000的LCM是最低的数字除以它们两者。
它包含k个或多个零(因为它是10 k的倍数),并且也将是n的倍数。
下面是实现:

C++
// CPP code to print K-rounded value of n
#include 
using namespace std;
 
// Function to compute the rounded value
long long getRounding(long long n, long long k)
{
    long long rounding = pow(10, k);
 
    // Computing GCD
    long long result = __gcd(rounding, n);
 
    // Returning LCM (GCD * LCM = n * k)
    return ((rounding * n) / result);
}
 
// Driver Code
int main()
{
 
    long long n = 375, k = 4;
 
    // Function call
    cout << getRounding(n, k);
 
    return 0;
}


Java
// JAVA Code For Smallest number divisible by
// n and has at-least k trailing zeros
import java.util.*;
 
class GFG {
     
     // Function to find gcd
     static long gcd(long a, long b)
        {
            // Everything divides 0
            if (a == 0 || b == 0)
               return 0;
           
            // base case
            if (a == b)
                return a;
           
            // a is greater
            if (a > b)
                return gcd(a-b, b);
            return gcd(a, b-a);
        }
 
    // Function to compute the rounded value
    public static long getRounding(long n, long k)
    {
        long rounding = (long)Math.pow(10, k);
      
        // Computing GCD
        long result = gcd(rounding, n);
      
        // Returning LCM (GCD * LCM = n * k)
        return ((rounding * n) / result);
    }
     
    /* Driver program to test above function */
    public static void main(String[] args)
    {
        long n = 375, k = 4;
          
        // Function call
        System.out.println( getRounding(n, k));
         
    }
}
   
// This code is contributed by Arnav Kr. Mandal.


Python3
# python Code For Smallest number
# divisible by n and has
# at-least k trailing zeros
 
# Function to find gcd
def gcd(a, b):
     
    # Everything divides 0
    if (a == 0 or b == 0):
        return 0
             
    # base case
    if (a == b):
        return a
             
    # a is greater
    if (a > b):
        return gcd(a - b, b)
         
    return gcd(a, b - a)
         
# Function to compute the
# rounded value
def getRounding(n, k):
     
    rounding = pow(10, k);
 
    # Computing GCD
    result = gcd(rounding, n)
 
    # Returning LCM (GCD * LCM
    # = n * k)
    return ((rounding * n) / result)
 
# Driver Code
 
n = 375
k = 4
 
# Function call
print( int(getRounding(n, k)))
 
# This code is contributed by Sam007


C#
// C# Code For Smallest number
// divisible by n and has
// at-least k trailing zeros
using System;
 
class GFG {
     
    // Function to find gcd
    static long gcd(long a, long b)
        {
             
            // Everything divides 0
            if (a == 0 || b == 0)
            return 0;
             
            // base case
            if (a == b)
                return a;
             
            // a is greater
            if (a > b)
                return gcd(a - b, b);
            return gcd(a, b - a);
        }
 
    // Function to compute the rounded value
    public static long getRounding(long n, long k)
    {
        long rounding = (long)Math.Pow(10, k);
     
        // Computing GCD
        long result = gcd(rounding, n);
     
        // Returning LCM (GCD * LCM = n * k)
        return ((rounding * n) / result);
    }
     
    // Driver Code
    public static void Main()
    {
        long n = 375, k = 4;
         
        // Function call
        Console.Write( getRounding(n, k));
         
    }
}
     
// This code is contributed by Nitin Mittal.


PHP
 $b)
        return gcd($a - $b, $b);
    return gcd($a, $b - $a);
}
 
// Function to compute
// the rounded value
function getRounding($n, $k)
{
    $rounding = intval(pow(10, $k));
 
    // Computing GCD
    $result = gcd($rounding, $n);
 
    // Returning LCM (GCD * LCM = n * k)
    return intval(($rounding * $n) /
                   $result);
}
 
// Driver code
$n = 375;
$k = 4;
 
// Function call
echo getRounding($n, $k);
 
// This code is contributed by Sam007
?>


Javascript


输出 :

30000