leetcode/263.c
2024-09-08 14:39:20 +08:00

31 lines
561 B
C

#include <assert.h>
#include <stdbool.h>
/*
如果 `n` 为丑数则按数学定义可表示为 `n = 2^a * 3^b * 5^c` 的形式,
只需反复除以 2、3、5 即可。当 `a = b = c = 0` 时 `a = 1`。
*/
bool isUgly(int n)
{
if (n <= 0)
return false;
int num[] = {2, 3, 5};
for (int i = 0; i < 3; i++)
{
while (n % num[i] == 0)
n /= num[i];
}
return n == 1;
}
int main()
{
assert(isUgly(6) == 1);
assert(isUgly(1) == 1);
assert(isUgly(14) == 0);
assert(isUgly(8) == 1);
return 0;
}