本文是在学习中的总结,欢迎转载但请注明出处: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;
	}

}

Leetcode_263_Ugly Number的更多相关文章

  1. JavaScript Math和Number对象

    目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...

  2. Harmonic Number(调和级数+欧拉常数)

    题意:求f(n)=1/1+1/2+1/3+1/4-1/n   (1 ≤ n ≤ 108).,精确到10-8    (原题在文末) 知识点:      调和级数(即f(n))至今没有一个完全正确的公式, ...

  3. Java 特定规则排序-LeetCode 179 Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  4. Eclipse "Unable to install breakpoint due to missing line number attributes..."

    Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...

  5. 移除HTML5 input在type="number"时的上下小箭头

    /*移除HTML5 input在type="number"时的上下小箭头*/ input::-webkit-outer-spin-button, input::-webkit-in ...

  6. iOS---The maximum number of apps for free development profiles has been reached.

    真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...

  7. 有理数的稠密性(The rational points are dense on the number axis.)

    每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.

  8. [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  9. [LeetCode] Number of Boomerangs 回旋镖的数量

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

随机推荐

  1. 20 ViewPager Demo3指示器

    MainActivity.java package com.qf.day20_viewpager_demo3; import java.util.ArrayList; import java.util ...

  2. 1QPushButton的使用,QLineEdit的使用,设置组件位置,布局(QHBoxLayout,QGridLayout)

     1.新建一个空Qt项目 2 新建一个新的文件(右击项目à添加新文件) 3 配置pro文件属性 SOURCES += \ main.cpp QT += widgets gui 4 编写main.c ...

  3. Retrofit 2.0 超能实践(三),轻松实现文件/多图片上传/Json字符串

    文:http://blog.csdn.net/sk719887916/article/details/51755427 Tamic 简书&csdn同步 通过前两篇姿势的入门 Retrofit ...

  4. Servlet编程实例-servlet学习之旅(三)

    LoginServlet代码: public class LoginServlet extends HttpServlet{ @Override protected void service(Http ...

  5. 【安卓中的缓存策略系列】安卓缓存策略之磁盘缓存DiskLruCache

    安卓中的缓存包括两种情况即内存缓存与磁盘缓存,其中内存缓存主要是使用LruCache这个类,其中内存缓存我在[安卓中的缓存策略系列]安卓缓存策略之内存缓存LruCache中已经进行过详细讲解,如看官还 ...

  6. Android初级教程图片信息

    对图片常规信息要了解其性质.图片大小.像素.位图等等概念总结如下: 图片在计算机中的大小 图片的总大小 = 图片的总像素 * 每个像素占用的大小(图片的总像素=像素尺寸也就是分辨率,例如设定800*4 ...

  7. 【UNIX环境高级编程】文件 IO 操作 一 ( open | close | creat | lseek | write | read )

    博客地址 : http://blog.csdn.net/shulianghan/article/details/46980271 一. 文件打开关闭操作相关函数介绍 1. open 函数 (1) op ...

  8. HDU2612---(两次BFS)

    Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Nin ...

  9. Java:函数,类,数组之间的运用

    在我的demoe类中,我实现了以下方法: 这个类在另外一个文件,demoe.java中 public class Demoe { //计算一个数字因子的个数 public static int get ...

  10. Dynamics CRM2013 任务列表添加自定义按钮

    任务列表的command bar 上面添加自定义按钮如下 要注意的是此处的列表不是任务实体而是活动实体,如果你是在任务实体的home栏上面加那你永远看不见按钮的显示,但如果是要在任务的表单界面上加按钮 ...