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 作为结果。
代码:
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;
}
}
//循环思想判断是否为超级丑数
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
java算法之超级丑数的更多相关文章
- Java实现 LeetCode 313 超级丑数
313. 超级丑数 编写一段程序来查找第 n 个超级丑数. 超级丑数是指其所有质因数都是长度为 k 的质数列表 primes 中的正整数. 示例: 输入: n = 12, primes = [2,7, ...
- [Swift]LeetCode313. 超级丑数 | Super Ugly Number
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- Ugly number丑数2,超级丑数
[抄题]: [思维问题]: [一句话思路]:Long.valueOf(2)转换为long型再做 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图 ...
- Leetcode 313.超级丑数
超级丑数 编写一段程序来查找第n个超级丑数. 超级丑数是指其所有质因数都是长度为 k 的质数列表 primes 中的正整数. 示例: 输入: n = 12, primes = [2,7,13,19] ...
- 313 Super Ugly Number 超级丑数
编写一段程序来寻找第 n 个超级丑数.超级丑数是指其所有质因数都在长度为k的质数列表primes中的正整数.例如,[1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] ...
- [Leetcode] 第313题 超级丑数
一.题目描述 编写一段程序来查找第 n 个超级丑数. 超级丑数是指其所有质因数都是长度为 k 的质数列表 primes 中的正整数. 示例: 输入: n = 12, primes = [2,7,13, ...
- 超级丑数--用查找的api解决
质数又称素数.一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数:否则称为合数. 质因数(素因数或质因子)在数论里是指能整除给定正整数的质数.除了1以外,两个没有其他共同质因子的正 ...
- leetcode 264. 丑数 II 及 313. 超级丑数
264. 丑数 II 题目描述 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, ...
- Leetocde的两道丑数题目:264. 丑数 II➕313. 超级丑数
Q: A: 用变量记录已经✖2.✖3.✖5的元素下标i2.i3.i5.表示截止到i2的元素都已经乘过2(结果添加到序列尾部的意思),i3.i5同理.这样每次可以循环可以O(1)时间找到下一个最小的丑数 ...
随机推荐
- [易飞]设置导入导出规则-小BUG
易飞系统在系统设置中-有设置导入导出规则,进行数据导入导出. 测试一:导入录入交易对象.从A账套导出到B账套,OK没有问题. 测试二:设置采购单单据性质. 导出结果: 怎么回事?把所有单据性质都导出了 ...
- Bootstrap 简介: 创建响应式、移动项目的工具
原文链接: Introduction to Bootstrap: A Tool for Building Responsive, Mobile-First Projects 下载: 示例代码Boots ...
- Oracle Applications DBA 基础(一)
1.引子 2014年9月13日 20:33 <oracle Applications DBA 基础>介绍Oracle Applications R12的系统架构, 数据库后台及应用系统的基 ...
- PHP 与搜索蜘蛛
本文移到:http://www.phpgay.com/Article/detail/classid/2/id/63.html
- Understanding Android Security(安卓安全的理解)
论文作者: Enck, William Ongtang, MacHigar McDaniel, Patrick 下一代的开放操作系统不会在个人主机和大型主机上出现,而是在只能手机上.新环境的开放性将会 ...
- LeetCode之“树”:Path Sum && Path Sum II
Path Sum 题目链接 题目要求: Given a binary tree and a sum, determine if the tree has a root-to-leaf path suc ...
- Android开发常用网站汇总
1.eoe Android开发者论坛 目前国内最早的Android开发者社区,人气非常旺聚集了不少Android开发方面的高手,开发中遇到的问题大都能在这里获得解决,网站最大的特色是定期发布<e ...
- DOS窗口如何实现复制粘贴
最近很多时候直接ctrl+c和ctrl+v无法实现DOS中的复制与粘贴,自己输入很麻烦.就要选择其他方式.查找资源后,总结如下: 方法一:第一种方式:右键标记-->选中-->标题栏右键编辑 ...
- 更改EBS服务器域名/IP
more: 341322.1 : How to change the hostname of an Applications Tier using AutoConfig 338003.1 : How ...
- studio grandle渠道打包
1. Mainfest 文件中添加一个键值对,这里的value 我定义为 "UMENG_CHANNEL_VALUE"(当然实际应用中可以根据自己的需要命名),后面打包的时候会对这 ...