【英文题目】(学习英语的同时,更能理解题意哟~)
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 1:
Input:
Output: true
Explanation: = ×
Example 2:
Input:
Output: true
Explanation: = × ×
Example 3:
Input:
Output: false
Explanation: is not ugly since it includes another prime factor 7.
Note:
1
is typically treated as an ugly number.【中文题目】
编写一个程序判断给定的数是否为丑数。
丑数就是只包含质因数 2, 3, 5
的正整数。
示例 1:
输入:
输出: true
解释: = ×
示例 2:
输入:
输出: true
解释: = × ×
示例 3:
输入:
输出: false
解释: 不是丑数,因为它包含了另外一个质因数 。
说明:
1
是丑数。【思路】
只要num能被2、3、5整除,则除以2、3、5,最后判断num是否为1.
注意num=0时需特殊处理。
【代码】
python版本
class Solution(object):
def isUgly(self, num):
"""
:type num: int
:rtype: bool
"""
if num == :
return False
while num % == :
num /=
while num % == :
num /=
while num % == :
num /=
return num ==
C++版本
class Solution {
public:
bool isUgly(int num) {
if(num == )
return false;
while(num % == )
num /= ;
while(num % == )
num /= ;
while(num % == )
num /= ;
if(num == )
return true;
return false;
}
};