java算法之超级丑数

问题描述:

写一个程序来找第 n 个超级丑数。

超级丑数的定义是正整数并且所有的质数因子都在所给定的一个大小为 k 的质数集合内。

比如给你 4 个质数的集合 [2, 7, 13, 19], 那么 [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] 是前 12 个超级丑数。

注意事项

1:永远都是超级丑数不管给的质数集合是什么。

2:给你的质数集合已经按照升序排列。

0 < k ≤ 100, 0 < n ≤ 10^6, 0 < primes[i] < 1000

样例 :给出 n = 6 和质数集合 [2, 7, 13, 19]。

第 6 个超级丑数为 13,所以返回 13 作为结果。

 代码:

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #931a68 }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; min-height: 15.0px }
p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco }
span.s1 { color: #000000 }
span.s2 { color: #931a68 }
span.s3 { color: #7e504f }
span.s4 { color: #0326cc }
span.s5 { text-decoration: underline; color: #7e504f }
span.Apple-tab-span { white-space: pre }

package 超级丑数;

import java.util.ArrayList;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

System.out.println(3/2);

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

int k = sc.nextInt();

if(k<=0||k>100){

return;

}

int primes[] = new int[k];

for (int i = 0; i < primes.length; i++) {

primes[i] = sc.nextInt();

for (int j = 2; j < primes[i]/2+1; j++) {

if(primes[i]%j==0){

return;

}

}

}

ArrayList<Integer> list = new ArrayList<Integer>();

list.add(1);

for (int i = 2; i < Math.pow(10, 6); i++) {

if(isSuperUglyNumber(i,primes)){

list.add(i);

}

if(list.size()==n){

System.out.println(list.get(n-1));

break;

}

}

}

//递归思想判断是否为超级丑数

private static boolean isSuperUglyNumber(int i, int[] primes) {

if(i<primes[0])return false;

else{

for (int j = primes.length-1; j >= 0; j--) {

if(i==primes[j]){

return true;

}

else if(i>primes[j]&&i%primes[j]==0){

if(isSuperUglyNumber(i/primes[j],primes)){

return true;

}

}else{

continue;

}

}

return false;

}

}

//循环思想判断是否为超级丑数

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #931a68 }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco }
span.s1 { color: #000000 }
span.s2 { color: #7e504f }
span.s3 { color: #931a68 }
span.s4 { color: #0326cc }
span.s5 { text-decoration: underline; color: #7e504f }
span.Apple-tab-span { white-space: pre }

public static boolean isSuperUglyNumber(int num ,int[] p){

for (int i = 0; i < p.length; i++) {

int j = 0;

while(true){

if(num % p[i] != 0 ){

break;

}else{

num = num/p[i];

continue;

}

}

if(num == 1){

return true;

}

}

return false;

}

}

采用了循环和递归两种思想,两个方法都可以正确判断到是否为满足条件超级丑数,这里就不放测试结果了,请自行测试

还有一些细节上的问题没有处理,不要在意这些哈。

转载请注明出处哦http://www.cnblogs.com/meng1314-shuai/p/7233403.html
时间: 2024-08-24 22:27:35

java算法之超级丑数的相关文章

剑指Offer面试题34(java版):丑数

题目:丑数 * 我们把只包含因子2,3,5的数称为丑数(Ugly Number). * 求按从小到大的顺序的第1500个丑数. * 例如6,8都是丑数,但14不是,因为它含有因子7.习惯上我们把1当作第一个丑数 方法一:逐个判断每个整数是不是丑数的解法,直观但不够高效: 所谓一个数m是另一个数n的因子,是指n能被m整除,也就是说n%m==0.根据丑数的定义,丑数只能被2,3,5整除.也就是说如果一个数能被2整除,我们把它连续除以2:如果能被3整除,就连续除以3:如果能被5整除,就除以5.如果最后

超级丑数--用查找的api解决

class Ugly { constructor(n, primes) { this.n = n this.primes = primes } getAll() { // 超级丑数列表 let res = [1] let i = 2 let primes = this.primes // 不知道上限用while循环 while (res.length < this.n) { let arr = Ugly.getPrimies(i) let k = 0 let l = arr.length for

《剑指offer》---丑数

本文算法使用python3实现 1. 问题1 1.1 题目描述: ??把只包含因子2.3和5的数称作丑数(Ugly Number).判断一个数是否是丑数. ??时间限制:1s:空间限制:32768K 1.2 思路描述: ??大致思路:将该数依次除以 $ 2,3,5 $ ,若最后商为 $ 1 $ 则是丑数,否则,不是丑数. 1.3 程序代码: class Solution: def isUgly(self, num): '''判断num是否是丑数''' if num <= 0: return Fa

编程算法 - 丑数 代码(C)

丑数 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 我们把只包含因子2, 3 和 5的数称作丑数. 求按从小到大的顺序的第5个丑数. 可以设置一个数组包含所需要的丑数, 依次比较乘以2, 乘以3, 乘以5的最小的数, 最后返回结果. 如第5个丑数是5, 如1, 2, 3, 4(2*2), 5均是丑数. 代码: /* * main.cpp * * Created on: 2014.6.12 * Author: Spike */ /*ecli

笔试算法题(20):寻找丑数 &amp; 打印1到N位的所有的数

出题:将只包含2,3,5的因子的数称为丑数(Ugly Number),要求找到前面1500个丑数: 分析: 解法1:依次判断从1开始的每一个整数,2,3,5是因子则整数必须可以被他们其中的一个整除,如果不包含任何其他因子则最终的结果为1: 解法2:小丑数必然是某个大丑数的因子,也就是乘以2,3,或者5之后的值,所以可以利用已经找到的丑数来寻找下一个丑数,使用数组有序保存已经找到的丑 数,并且当前最大丑数值为M:用大于M/2的丑数乘以2得到M1,用大于M/3的丑数乘以3得到M2,用大于M/5的丑数

算法入门经典-第五章 例题5-7 丑数

#include<iostream> #include<vector> #include<queue> #include<set> using namespace std; typedef long long ll; const int coeff= {2,3,5}; int main() { //一些常见的优先队列,STL提供了更为简单的定义方法 //对于任意丑数x 则 2x,3x,5x也是丑数,判断一个丑数是否生成过 //每次取出最小的丑数,生成3个新的

leetcode 丑数 II java

题目: 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10输出: 12解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数.说明: 1 是丑数.n 不超过1690. 解题: 使用三指针 class Solution { public int nthUglyNumber(int n) { int min2 = 0; int min3 = 0; int min5 = 0; int []result =

lintcode 中等题:Ugly Numbers 丑数

题目 丑数 设计一个算法,找出只含素因子3,5,7 的第 k 大的数. 符合条件的数如:3,5,7,9,15...... 样例 如果k=4, 返回 9 挑战 要求时间复杂度为O(nlogn)或者O(n) 解题 法一:直接暴力,逐次判断一个数是不是丑数 下面只对其中的奇数判断是否是丑数,加不加奇数都超时. class Solution { /** * @param k: The number k. * @return: The kth prime number as description. */

寻找第K个丑数

把只包含质因子2.3和5的数称作丑数(Ugly Number),例如:2,3,4,5,6,8,9,10,12,15,等,习惯上我们把1当做是第一个丑数. 写一个高效算法,返回第n个丑数. import static java.lang.Math.min; import static java.lang.System.out; public class UglyNumber { public static void main(String[] args) { out.println(findKth