给出了整数X和K。任务是找到可被X整除的最高K位数字。
例子:
Input : X = 30, K = 3
Output : 990
990 is the largest three digit
number divisible by 30.
Input : X = 7, K = 2
Output : 98
一个有效的解决方案是使用以下公式。
ans = MAX - (MAX % X)
where MAX is the largest K digit
number which is 999...K-times
该公式适用于简单的学校方法划分。我们删除余数以得到最大的可除数。
// Java code to find highest
// 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 MAX
double MAX = Math.pow(i, K) - 1;
// returning ans
return (MAX - (MAX % X));
}
public static void main(String[] args)
{
// Number whose divisible is to be found
double X = 30;
// Max K-digit divisible is to be found
double K = 3;
System.out.println((int)answer(X, K));
}
}
// Code contributes by Mohit Gupta_OMG <(0_o)>
输出:
990
要了解Math.pow()函数,请参考本文的第18点:
https://www.geeksforgeeks.org/java-lang-math-class-java-set-2/
请参阅有关被X整除的最大K位数字的完整文章,以了解更多详细信息!