您的位置:首页 > 其它

LeetCode 263----Ugly Number

2015-09-09 17:03 417 查看
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.

只能被2、3、5整除的数,不断除2、3、5后,余数为1,余数为其它时,则不是-Ugly Number

class Solution {
public:
bool isUgly(int num) {
int x=num;
if(x<=0)
return false;
while(x>=2)
{
if(x%2==0)
x=x/2;
else
if(x%3==0)
x=x/3;
else
if(x%5==0)
x=x/5;
else
return false;
}
if(x==1)
return true;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: