UglyNumber - 找“丑数”
uglynumber的定义是只能被1,2,3,5整除的数
规定1是第一个uglynumber;以此类推,1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36 40 ...
问题1,给定一个非零int整数,判断是否为ugly number
此数除以2,3,5;看是否能被整除;整除的判断是用hasDigit方法,利用小数来判断的
如果能被整除,递归下去,再除以2,3,5;直到完成判断
/**
* Ugly number1
* @author Rust Fisher
* Judge the number whether ugly
*/
public class UglyNumber1 {
public static boolean hasDigit(double d){
return d*10%10 != 0;
}
/**
* @param num
* @return boolean whether num is ugly
*/
public static boolean isUgly(int num) {
if (num <= 0) {
return false;
}
if (num == 1) {
return true;
}
if (!hasDigit(num/2.0)) {
if (isUgly(num/2)) {
return true;
}
}
if (!hasDigit(num/3.0)) {
if (isUgly(num/3)) {
return true;
}
}
if (!hasDigit(num/5.0)) {
if (isUgly(num/5)) {
return true;
}
}
return false;
}
/**
* Find the nth ugly number
* @param n
* @return the nth ugly number
*/
public static int nthUglyNumber(int n) {
if (n <= 0) {
return -1;
}
int count = 0;
int i = 0;
while (count <= n){
if (isUgly(i)) {
count++;
}
if (count == n) {
break;
}
i++;
}
return i;
}
public static void main(String args[]){
int count = 0;
for (int i = 0; i < 100; i++) {
if (isUgly(i)) {
count++;
System.out.print(i + "\t");
}
if (count == 10) {
count = 0;
System.out.println();
}
}
System.out.println("\nThe n-th ugly numbers : ");
count = 0;
for (int i = 1; i < 21; i++) {
System.out.print(nthUglyNumber(i) + " ");
}
System.out.println("\n用这种方式输出第n个ugly number很没效率");
}
}
输出:
1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36 40 45 48 50 54 60 64 72 75 80 81 90 96 The n-th ugly numbers : 1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36 用这种方式输出第n个ugly number很没效率
问题2:求第n个ugly number
比如第1个ugly number是1,第二个是2,第三个是3 ...
已知1,2,3,5是ugly number,那么接下去的数能够乘以2,3,5得到;一个一个向后推算,直到第n个
设定3个游标,对应因数为2,3,5;利用到因数2一次,index2加一
public class UglyNumber2{
public static int getMin(int a,int b,int c){
int min = a < b ? a : b;
return min < c ? min : c;
}
public static int nthUglyNumber(int n) {
if (n < 1) {
return -1;
}
int index2 = 0, index3 = 0, index5 = 0; // three index
int[] uglyNums = new int[n];
uglyNums[0] = 1;
int next = 1;
while (next < n) {
uglyNums[next] = getMin(uglyNums[index2]*2,uglyNums[index3]*3,uglyNums[index5]*5);
if (uglyNums[next] == uglyNums[index2]*2) index2++;// find out which index should move
if (uglyNums[next] == uglyNums[index3]*3) index3++;// index moving forward
if (uglyNums[next] == uglyNums[index5]*5) index5++;
next++;
}
return uglyNums[next - 1];
}
public static void main(String args[]){
for (int i = 1; i < 21; i++) {
System.out.print(nthUglyNumber(i) + " ");
}
}
}
输出:
1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36
输出了前20个ugly number
UglyNumber - 找“丑数”的更多相关文章
- 剑指offer系列59---寻找丑数
[题目]把只包含因子2.3和5的数称作丑数(Ugly Number). * 例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 解法一 ...
- 4 丑数 Ⅱ-找出第n个丑数
原题网址:http://www.lintcode.com/zh-cn/problem/ugly-number-ii/ 设计一个算法,找出只含素因子2,3,5 的第 n 小的数. 符合条件的数如:1, ...
- 剑指offer-第5章优化时间和空间效率(丑数)
题目:我们把只包含因子2,3,5的数叫做丑数.寻找第1500个丑数.通常把1当成第一个丑数. 思路1:第一步判断是否为丑数:丑数是只包含2,3,5的数,因此一定可以被2,3,5整除.通过求余数是否为零 ...
- 37.寻找丑数[Ugly numbers]
[题目] 我们把只包含质因子2.3和5的数称作丑数(Ugly Number),例如:2,3,4,5,6,8,9,10,12,15,等,习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第1500个丑 ...
- 洛谷P2723 丑数 Humble Numbers
P2723 丑数 Humble Numbers 52通过 138提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交 讨论 题解 最新讨论 暂时没有讨论 题目背景 对于一给定的素数 ...
- lintcode :Ugly Numbers 丑数
题目 丑数 设计一个算法,找出只含素因子3,5,7 的第 k 大的数. 符合条件的数如:3,5,7,9,15...... 样例 如果k=4, 返回 9 挑战 要求时间复杂度为O(nlogn)或者O(n ...
- 剑指OFFER之丑数(九度OJ1214)
题目描述: 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7.习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 输入: 输 ...
- 丑数 LeeTCode
题目链接:http://www.lintcode.com/zh-cn/problem/ugly-number-ii/ 题目描述:设计一个算法,找出只含素因子2,3,5 的第 n 大的数.符合条件的数如 ...
- Humble Numbers(丑数) 超详解!
给定一个素数集合 S = { p[1],p[2],...,p[k] },大于 1 且素因子都属于 S 的数我们成为丑数(Humble Numbers or Ugly Numbers),记第 n 大的丑 ...
随机推荐
- profiler内存优化:警惕回调函数
最近做profiler内存优化,踩了一个深坑,觉得有必要做一下笔记. 过程是这样的,游戏启动后,会启动更新模块,加载更新界面,更新检测完成后会切换场景进入登陆界面.切换场景会自动释放上一个场景的资源. ...
- A comparison of local caches (2) 【本地缓存之比较 (2)】
接上一篇: A comparison of local caches (1) [本地缓存之比较 (1)] This article will compare the asynchronous loca ...
- APUE-文件和目录(六)函数ftw和nftw
名字 ftw,nftw - 文件树遍历 概要 #include <ftw.h> int nftw(const char *dirpath, int (*fn) (const char *f ...
- Oracle查询数据出来乱码问题?
为什么Oracle 查询出来的数据会产生乱码? 安装的数据库和客户端编码编码不一致就会产生乱码,要想解决此问题改一下客户端的编码即可 1. select * from table; 如果是这种问题 ...
- 浅谈this那些事
一直以来,对this的讨论都是热门话题.有人说掌握了this就掌握了JavaScript的80%,说法有点夸张,但可见this的重要性.本人至今也是记录了很多关于this的零碎笔记,今天就来个小结. ...
- JS事件及其兼容用法
JavaScript与HTML之间的交互是通过事件实现的.事件,就是文档或浏览器窗口中发生的一些特定的交互瞬间. 1.事件流:描述的是从页面中接收事件的顺序. IE提出的事件冒泡流:事件开始由最具体的 ...
- 解决arcgis javascript textsymbol不支持多行文本标注的问题
首先,下载这段js文件,命名为esri.symbol.MultiLineTextSymbol.js require(["esri/layers/LabelLayer"], func ...
- fpm 制作 rpm 包
支持的 源类型包 ① dir : 将目录打包成所需要的类型, 可用于源码编译安装软件包 ② rpm : 对 rpm 包进行转换 ③ gem : 对 rubygem 包进行转换 ④ python : 将 ...
- Java线程池入门必备
线程池 一. 线程池的简介 1.什么是线程池? 最早期的工作线程处理任务的模型.一个任务的到来,会伴随着线程的创建,当处理完任务后,线程会被销毁,资源回收.这种一个任务一个线程一系列创建销毁的模式 ...
- 警惕System.Environment.CurrentDirectory 获取当前目录
最近工作中,要做个客户端提醒的小工具:winform程序自然少不了要读取和应用程序同一个目录的配置文件(不是exe.config文件): 要读取当前应用程序所在目录我立马想到了System.Envir ...