Problem
Write a program to check whether a given number is an ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.
Example
Input: 6
Output: true
Explanation: 6 = 2 × 3
Input: 8
Output: true
Explanation: 8 = 2 × 2 × 2
Input: 14
Output: false
Explanation: 14 is not ugly since it includes another prime factor 7.
Code
static int var = [](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();
class Solution {
public:
bool isUgly(int num) {
if(num<1)
return false;
while(1){
if(num==1)
return true;
else if(num%5==0){
num=num/5;
continue;
}
else if(num%3==0){
num=num/3;
continue;
}
else if(num%2==0){
num=num/2;
continue;
}
else
return false;
}
}
};
网友评论