263.Ugly Number
给定一个数,判断其质因数是否是2,3,5。
Input: 6
Output: true
Explanation: 6 = 2 × 3
Input: 14
Output: false
Explanation: 14 is not ugly since it includes another prime factor 7.
思路:对2,3,5取余判断,可以先分别对2,3,5取余,再判断,减少重复判断次数。
bool isUgly(int num) { if (num <= 0) return false; if (num == 1) return true; while (num > 1) { if (num % 2 == 0) num /= 2; if (num % 3 == 0) num /= 3; if (num % 5 == 0) num /= 5; if (num != 1 && num % 2 != 0 && num % 3 != 0 && num % 5 != 0) return false; } return true; }