[263] Ugly number
2015-08-20 16:52
253 查看
1. 题目描述
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.
Ugly number代表能且只能被2,3,5整除的正整数,也就是说这个数是任一个2,3,5相乘得到的结果。
2. 解决方案
根据题目描述可以看出只要将这个数一直对2,3,5三个数做除法直到不能整除时考察结果是否为1即可,下面给出实现。3. Code
class Solution { public: bool isUgly(int num) { if(num <= 0) // 判定是否满足正整数 return false; else { bool flag = false; // 使num一直对2,3,5做除法直到无法整除 do { flag = false; if(num%2 == 0){ // num整除2 num/=2; flag = true; } if(num%3 == 0){ // num整除3 num/=3; flag = true; } if(num%5 == 0){ // num整除5 num/=5; flag = true; } }while(flag); if(num == 1) // 若能完全被2,3,5整出则结果为1 return true; return false; } } };
相关文章推荐
- Office 365 IT Professional——利用PowerShell管理Administrator Role
- mysql的mysqldump用法,重置数据库
- python __name__ == '__main__'解析
- 中缀表达式转换成前缀表达式和后缀表达式的极其简单方法【转】
- 安卓突击:ANR
- Windows 10企业批量部署实战之系统分区设置
- Windows 10企业批量部署实战之系统分区设置
- 装数据库的准备工作
- java序列化
- SWFUpload多图上传代码
- 关于eclipse死活不能编译java生成class
- Gas Station
- 可以ping通无法通过浏览器浏览该网站
- 虚拟化--024 vm network在哪个位置设置混杂模式
- 【codevs1048】【codevs1154&&06TG】石子归并、能量项链,序列dp的典型题目
- CheckBoxList去分号绑定选中值
- Java学习--(十)final关键字
- HDU 4109 Instrction Arrangement拓扑排序 关键路径模板
- Mac 使用adb shell 命令
- SharePoint 2013 通过HttpModule 管理视图权限