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. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.

Note:

1 is typically treated as an ugly number.
Input is within the 32-bit signed integer range.
按照题目意思最开始原版代码(臃肿):

bool isUgly(int num) 
{
    if(1 == num)
        return true;
    if(0 == num)
        return false;
    
    while(num)
    {
        if(1 == num)
            return true;
        if(num %2 ==0)
            num /= 2;
        else if (num %3 ==0)
            num /= 3;
        else if (num %5 ==0)
            num /= 5;
        else 
            return false;
    }
    return true;
    
}

经过整理之后比较简洁的代码,将num =0和num =1的情况一并处理,形式上更加清晰。例如for循环中的&&num是考虑了num =0的情况下,for条件不成立,直接return num == 1。
num=1的情况是循环中while条件都不成立,然后最后直接返回return num == 1

bool isUgly(int num) 
{    
    for (int i=2; i<6 && num; i++)
    {
        while (num % i == 0)
        {
            num /= i;
        }
    }
    
    return num == 1;
    
}