Leetcode_263_Ugly Number
2015-10-26 23:04
357 查看
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/49431329
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
System.err.println(i + "==" + isUgly(i));
}
}
// 2 3 5
public static boolean isUgly(int num) {
if (num == 1)
return true;
while (num >= 2 && num % 2 == 0) {
num = num / 2;
}
while (num >= 3 && num % 3 == 0) {
num = num / 3;
}
while (num >= 5 && num % 5 == 0) {
num = num / 5;
}
return num == 1 ? true : false;
}
}
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, 8are ugly while
14is not ugly since it includes another prime factor
7.Note that
1is typically treated as an ugly number.思路:(1)该题题意为给定一个数,判断该数是否为“丑数”。(2)这道题很基础。丑数是因子只含有2、3、5的数。所以只需对这三个数循环取余判断即可。这里不再累赘。(3)详情见下方代码。希望本文对你有所帮助。public class Ugly_Number {
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
System.err.println(i + "==" + isUgly(i));
}
}
// 2 3 5
public static boolean isUgly(int num) {
if (num == 1)
return true;
while (num >= 2 && num % 2 == 0) {
num = num / 2;
}
while (num >= 3 && num % 3 == 0) {
num = num / 3;
}
while (num >= 5 && num % 5 == 0) {
num = num / 5;
}
return num == 1 ? true : false;
}
}
相关文章推荐
- [BZOJ1650][Usaco2006 Dec]River Hopscotch 跳石子
- sqlie3 Replace into
- Leetcode_263_Ugly Number
- NSData 和NSArray之间的转换
- Linux(Ubuntu)下如何安装JDK
- 【HDOJ】3234 Exclusive-OR
- 强联通分量
- POJ 3083 Children of the Candy Corn(BFS + DFS)
- python socket and SSL example
- Sterling B2B Integrator与SAP交互 - 02 安装配置
- C++:异常的处理
- 解释器模式
- 系统系统项目管理工程师20151026作业
- Windows10 CodeWarrior安装
- Saltstack系列(二) Saltstack分组
- 团队作业Week5之团队贡献分的分配
- JVM参数配置
- staitc用于函数内部和函数外部的不同
- android-----(Content Provider 内容提供者组件)
- hdu 1080 Human Gene Functions 线性dp