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.

Notice

Note that 1 is typically treated as an ugly number.

Example
Given num = 8 return true
Given num = 14 return false

LeetCode上的原题,请参见我之前的博客Ugly Number

解法一:

class Solution {
public:
/**
* @param num an integer
* @return true if num is an ugly number or false
*/
bool isUgly(int num) {
while (num > ) {
if (num % == ) num /= ;
else if (num % == ) num /= ;
else if (num % == ) num /= ;
else return false;
}
return num == ;
}
};

解法二:

class Solution {
public:
/**
* @param num an integer
* @return true if num is an ugly number or false
*/
bool isUgly(int num) {
if (num <= ) return false;
while (num % == ) num /= ;
while (num % == ) num /= ;
while (num % == ) num /= ;
return num == ;
}
};

[LintCode] Ugly Number 丑陋数的更多相关文章

  1. [LeetCode] Ugly Number 丑陋数

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  2. [LeetCode] 263. Ugly Number 丑陋数

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  3. Ugly number丑数2,超级丑数

    [抄题]: [思维问题]: [一句话思路]:Long.valueOf(2)转换为long型再做 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图 ...

  4. lintcode:Ugly Number I

    Ugly Number Write a program to check whether a given number is an ugly number`. Ugly numbers are pos ...

  5. [LintCode] Happy Number 快乐数

    Write an algorithm to determine if a number is happy. A happy number is a number defined by the foll ...

  6. 263 Ugly Number 丑数

    编写程序判断给定的数是否为丑数.丑数就是只包含质因子 2, 3, 5 的正整数.例如, 6, 8 是丑数,而 14 不是,因为它包含了另外一个质因子 7.注意:    1 也可以被当做丑数.    输 ...

  7. [LeetCode] Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  8. [LeetCode] 264. Ugly Number II 丑陋数 II

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  9. [LeetCode] 313. Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

随机推荐

  1. Codeforces Round #212 (Div. 2) D. Fools and Foolproof Roads 并查集+优先队列

    D. Fools and Foolproof Roads   You must have heard all about the Foolland on your Geography lessons. ...

  2. c++11 正则表达式基本使用

    c++ 11 正则表达式 常用的方法 regex_match regex_search regex_replace 等. regex_match 要求正则表达式必须与模式串完全匹配,例如: strin ...

  3. 系统剖析Android中的内存泄漏

    [转发]作为Android开发人员,我们或多或少都听说过内存泄漏.那么何为内存泄漏,Android中的内存泄漏又是什么样子的呢,本文将简单概括的进行一些总结. 关于内存泄露的定义,我可以理解成这样 没 ...

  4. 浅谈C++多态性

    本文转载至http://blog.csdn.net/hackbuteer1/article/details/7475622 总结: (1)区分概念:    重载----同一个类中,相同的函数名字,不同 ...

  5. Android拓展系列(12)--使用Gradle发布aar项目到JCenter仓库

    目的 发布自己的android library(也就是aar)到公共的jcenter仓库,所有的人都能用gradle最简单的方式引用. 为什么选择jcenter,它兼容maven,而且支持更多形式仓库 ...

  6. 《DSP using MATLAB》示例Example4.9

    收敛域在圆外,对应原始时间序列为右边序列. 上代码: b = 1; a = poly([0.9, 0.9, -0.9]); % compute the polynomials coefficients ...

  7. javascript优化--14模式2(DOM和浏览器模式)

    远程脚本 XMLHttpRequest JSONP 和XHR不同,它不受同域的限制: JSONP请求的可以是任意的文档: 请求的URL通常格式为http://example.js?calback=Ca ...

  8. express-12 Cookie与会话

    简介 HTTP是无状态协议.当浏览器中加载页面,然后转到同一网站的另一页面时,服务器和浏览器都没有任何内在的方法可以认识到,这是同一浏览器访问同一网站.换一种说法,Web工作的方式就是在每个HTTP请 ...

  9. css -- 元素消失

    元素从屏幕消失的方法: A:display:none B:opacity C:visibility D:text-intent:-10000em; E:margin可远可远了 F:position:a ...

  10. css3 -- 伪类与伪元素

    伪类: 1.结构伪类 A:E : first-child{} E : nth-*(n){} E : first-*(even){}  E : first-*(odd){} B:nth-child 是根 ...