📌  相关文章
📜  Java程序,最小的K位数字可被X整除

📅  最后修改于: 2021-05-04 16:53:19             🧑  作者: Mango

给出了整数X和K。任务是找到可被X整除的最小K位数字。

例子:

Input : X = 83, K = 5
Output : 10043
10040 is the smallest 5 digit
number that is multiple of 83.

Input : X = 5, K = 2
Output : 10

一个有效的解决方案是:

Compute MIN : smallest K-digit number (1000...K-times)
If, MIN % X is 0, ans = MIN
else, ans = (MIN + X) - ((MIN + X) % X))
This is because there will be a number in 
range [MIN...MIN+X] divisible by X.
// Java code to find smallest K-digit
// number divisible by X
  
import java.io.*;
import java.lang.*;
  
class GFG {
    public static double answer(double X, double K)
    {
        double i = 10;
        // Computing MIN
        double MIN = Math.pow(i, K - 1);
  
        // returning ans
        if (MIN % X == 0)
            return (MIN);
        else
            return ((MIN + X) - ((MIN + X) % X));
    }
  
    public static void main(String[] args)
    {
  
        // Number whose divisible is to be found
        double X = 83;
        double K = 5;
  
        System.out.println((int)answer(X, K));
    }
}
  
// Code contributed by Mohit Gupta_OMG <(0_o)>
输出:
10043

要了解Math.pow()函数,请参考本文的第18点:
https://www.geeksforgeeks.org/java-lang-math-class-java-set-2/

请参阅有关被X整除的最小K位数字的完整文章,以了解更多详细信息!