Ugly Number
2016-07-05 07:09
246 查看
Write a program to check whether a given number is an ugly number.
Ugly numbers are positive numbers whose prime factors only include
Note that
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
思路:除以2,3,5直到不能除为止,最后应该是1,要为其他数,则不是ugly number.
Ugly numbers are positive numbers whose prime factors only include
2, 3, 5. For example,
6, 8are ugly while
14is not ugly since it includes another prime factor
7.
Note that
1is 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直到不能除为止,最后应该是1,要为其他数,则不是ugly number.
public class Solution { public boolean isUgly(int num) { if(num <= 0) return false; while(num!= 0 && num%2 ==0) { num = num/2; } while(num != 0 && num %3 == 0){ num = num/3; } while(num != 0 && num %5 == 0){ num = num/5; } return num == 1; } }
相关文章推荐
- Shortest Distance from All Buildings
- Asp.Net Core 发布和部署( MacOS + Linux + Nginx )
- (4) 百度2011研发工程师笔试卷
- SQL Server 存储过程
- 226. Invert Binary Tree
- PL/SQL Developer如何连接64位的Oracle图解
- SQL 触发器的作用,好处和坏处
- House Robber
- Windows驱动开发之驱动开发环境搭建
- SQL中declare申明变量
- SQL中declare申明变量
- html5调用摄像头拍照
- hive的查询注意事项以及优化总结 .
- SQL中的left outer join,inner join,right outer join用法
- Remove Duplicates from Sorted Array
- 源码推荐(0705):简仿映客直播App,UITableView右侧索引
- 这几天已经连续轰炸一个开锁的人电话短信无数次
- JavaScript事件使用指南
- Maximum Product of word lengths
- Generalized Abbreviation