LeetCode263——Ugly Number
2015-08-24 21:35
441 查看
Write a program to check whether a given number is an ugly number.
Ugly numbers are positive numbers whose prime factors only include
For example,
not ugly since it includes another prime factor
Note that
实现:
class Solution {
public:
bool isUgly(int num) {
if (num == 0) return false;
if (num == 1) return true;
if (num % 2 == 0) return isUgly(num/2);
else if (num % 3 == 0) return isUgly(num/3);
else if (num % 5 == 0) return isUgly(num/5);
return false;
}
};
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.
实现:
class Solution {
public:
bool isUgly(int num) {
if (num == 0) return false;
if (num == 1) return true;
if (num % 2 == 0) return isUgly(num/2);
else if (num % 3 == 0) return isUgly(num/3);
else if (num % 5 == 0) return isUgly(num/5);
return false;
}
};
相关文章推荐
- iOS中将汉字转换成拼音的方法
- 解释(n&(n-1))==0的具体含义
- C语言基础--循环 递归打印乘法表
- bootstrap-js(2)下拉菜单
- 可变参数的实现
- PhotoView的异常问题
- 内联函数详解--C++
- 使用Android Studio和Gradle编译NDK项目之Experimental Plugin User Guide
- getopt、getopt_long、getopt_long_only使用实例
- iOS图片轮播
- 大龄屌丝自学笔记--Java零基础到菜鸟--010
- 不等式估计
- 随机生成1-100之间的数,并无一重复的存入长度为100的数组中
- ubuntu 14.04安装opencv3.0.0
- C# windows服务简介
- 八大排序算法
- 找出二进制表示中1的个数相同,且大小最接近的那两个数
- swift详解之十-------------异常处理、类型转换 ( Any and AnyObject )
- poj2112 Optimal Milking --- 最大流量,二分法
- POJ3176--Cow Bowling