Input: n =10, t =2Output: 10Explanation:
The digit product of 10is0, which is divisible by 2, making it the smallest
number greater than or equal to 10 that satisfies the condition.
Input: n =15, t =3Output: 16Explanation:
The digit product of 16is6, which is divisible by 3, making it the smallest
number greater than or equal to 15 that satisfies the condition.
Since n and t are small, we can simply check each number starting from n, compute the product of its digits, and return the first number whose product is divisible by t.
classSolution {
public:int findInteger(int n, int t) {
for (int x = n; ; ++x) {
int prod =1, y = x;
while (y) { prod *= y %10; y /=10; }
if (prod % t ==0) return x;
}
}
};
1
2
3
4
5
6
7
8
9
classSolution {
publicintfindInteger(int n, int t) {
for (int x = n; ; ++x) {
int prod = 1, y = x;
while (y > 0) { prod *= y % 10; y /= 10; }
if (prod % t == 0) return x;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution:
deffindInteger(self, n: int, t: int) -> int:
x = n
whileTrue:
prod =1 y = x
while y:
prod *= y %10 y //=10if prod % t ==0:
return x
x +=1