欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

263. Ugly Number

程序员文章站 2022-04-24 15:34:34
...

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 that 1 is typically treated as an ugly number.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

思路:
丑数,只包含因子2、3、5的数叫丑数

Java 代码(程序写得不好):

public class Solution {
    public boolean isUgly(int num) {
        int[] arr = {2, 3, 5};
        while(num > 1) {
            int i = 0;
            for(; i < 3; i++) {
                if(num % arr[i] == 0) {
                    num /= arr[i];
                    break;
                }
            }
            if(i == 3) {
                return false;
            }
        }
        return num == 1;
    }
}

263. Ugly Number