Ugly Number
2015-10-27 16:04
218 查看
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.
class Solution {
public:
bool isUgly(int num)
{
if(num == 0)
return false;
int tmp = num;
while(tmp % 2 == 0)
tmp = tmp/2;
while(tmp % 3 == 0)
tmp = tmp/3;
while(tmp % 5 == 0)
tmp = tmp/5;
if(tmp != 1)
{
return false;
}
return true;
}
};
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.
class Solution {
public:
bool isUgly(int num)
{
if(num == 0)
return false;
int tmp = num;
while(tmp % 2 == 0)
tmp = tmp/2;
while(tmp % 3 == 0)
tmp = tmp/3;
while(tmp % 5 == 0)
tmp = tmp/5;
if(tmp != 1)
{
return false;
}
return true;
}
};
相关文章推荐
- CI 验证码辅助函数
- winscp中文乱码
- 在一个新建文件夹下初始化Git并下载代码的命令
- android 广告栏 viewpager
- Codeforces 591 B Rebranding【Codeforces Round #327 (Div. 2)】
- 20135234马启扬-——信息安全系统设计基础第七周学习总结
- 服务器租用如何选择
- android .9.png ”点九” 图片制作方法
- 删除链表中倒数第n个节点(LintCode)
- 小区IPTV新方案
- Region Proposal Networks
- 043.@protocol 正式协议
- js typeof
- jQuery学习之旅 Item5 $与jQuery对象
- vim 查找替换总结
- ubuntu14.4设置vi编辑器显示行号和代码颜色
- 019 Remove Nth Node From End of List [Leetcode]
- /opt/linux/linux-3.5/scripts/kconfig/Makefile:21: recipe for target 'menuconfig' failed错误
- 五、推导数据 ----- 处理数据
- 遮挡判断(模拟)