您的位置:首页 > 其它

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 
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.思路:(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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: