问题描述:

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.

分析:一个数,因式分解后,素数因子若只包含2/3/5,称为是ugly number;否则,不是ugly number。

给定一个数,判定它是不是ugly number。

算法:注意将 <=0的数过滤掉

    /**
* 判断一个数是不是ugly number
* @param num
* @return
*/ //将num除去2,3,5这三个因子后,应该得到最终结果为1,若不为1,就不是ugly number
public static boolean isUgly(int num) {
if(num <= 0) //题目要求是整数,所以对于非整数一定要进行排除,否则会超时
return false;
while(num % 2 == 0)//如果存在2的质因子,则将所有2都找出来
num = num / 2;
while(num % 3 == 0)//接着找出所有的3
num = num / 3;
while(num % 5 == 0)//找出所有的5
num = num / 5;
return (num == 1) ;//若除了2,3,5之外,不存在其他质数因子,则为ugly number }

最新文章

  1. js 字符串转换成数字的三种方法
  2. struts2学习笔记之二:基本环境搭建
  3. Android中突发情况Activity数据的保存和恢复
  4. Linux C 程序 空语句-gcc编译命令(SIX)
  5. Android Butterknife框架配置
  6. rotatelogs分割apache日志文件
  7. Luogu3373【模板】线段树2
  8. 201521123028 《Java程序设计》第8周学习总结
  9. Shell 变量详解教程之位置变量与预定义变量。
  10. tomcat在centos下的操作
  11. 数据结构树之AVL树(平衡二叉树)
  12. python 中 __init__方法
  13. react-native “Unable to resolve module &#39;AccessibilityInfo&#39;” 的解决方案
  14. static成员函数不能调用non-static成员函数
  15. CodeForces - 1097D:Makoto and a Blackboard (积性)
  16. TcpListener、TcpClient
  17. 【ASP.NET】IHttpHandler和IHttpModule
  18. darknet53 yolo 下的识别训练
  19. 域名解析的DNS缓存如何清理
  20. 基于jetty镜像的ossfs镜像docker镜像构建

热门文章

  1. Python hasattr() 函数 // python中hasattr()、getattr()、setattr()函数的使用
  2. Sql 通过表名查找所有列名
  3. 题解——HDU 1848 Fibonacci again and again
  4. vue中find函数
  5. 【ASP.NET】System.Web.Routing - RouteCollection Class
  6. 【译】第40节---EF6-命令监听
  7. new和malloc的用法和区别
  8. 线程中为控件赋值Winform
  9. JAVA读取CSV文件到MySQL数据库中
  10. _attribute_character