当前位置:网站首页>313. 超级丑数-暴力解法
313. 超级丑数-暴力解法
2022-08-09 02:22:00 【Mr Gao】
313. 超级丑数
超级丑数 是一个正整数,并满足其所有质因数都出现在质数数组 primes 中。
给你一个整数 n 和一个整数数组 primes ,返回第 n 个 超级丑数 。
题目数据保证第 n 个 超级丑数 在 32-bit 带符号整数范围内。
示例 1:
输入:n = 12, primes = [2,7,13,19]
输出:32
解释:给定长度为 4 的质数数组 primes = [2,7,13,19],前 12 个超级丑数序列为:[1,2,4,7,8,13,14,16,19,26,28,32] 。
示例 2:
输入:n = 1, primes = [2,3,5]
输出:1
解释:1 不含质因数,因此它的所有质因数都在质数数组 primes = [2,3,5] 中。
解题代码如下:
bool find(int val,int* primes, int primesSize){
if(val==1){
return true;
}
for(int i=0;i<primesSize;i++){
if(primes[i]==val){
return true;
}
}
return false;
}
bool re(int n){
if(n==1){
return true;
}
for(int i=2;i<=n/2;i++){
if(n%i==0){
return false;
}
}
return true;
}
int nthSuperUglyNumber(int n, int* primes, int primesSize){
int count=0;
// for(int i=1;i<10;i++){
// if(re(i)){
// printf("%d ",i);
// }
// }
for(int i=1;i<100000;i++){
// printf("--%d ",i);
int r=0;
if(i==1){
count++;
if(count==n){
return i;
}
}
else{
for(int j=1;j<=i/2;j++){
if(i%j==0){
if(re(j)){
// printf("|%d ",j);
if(find(j,primes,primesSize)){
}
else{
r=1;
break;
}
}
if(re(i/j)){
// printf("|%d ",i/j);
if(find(i/j,primes,primesSize)){
}
else{
r=1;
break;
}
}
}
}
if(r==0){
count++;
if(count==n){
return i;}
// printf("count %d %d",i,count);
}
}
}
return 0;
}
边栏推荐
猜你喜欢

Mysql 5.7 into the pit

ROS2 ERROR: OpenGL 1.5 is not supported in GLRenderSystem::initialiseContext at C:\ci\ws\build...

eladmin container deployment super detailed process

mysql 5.7 入坑

2022年自然语言处理校招社招实习必备知识点盘点分享

史上最猛“员工”,疯狂吐槽亿万富翁老板小扎:那么有钱,还总穿着同样的衣服!

D. Tournament Countdown

The first lesson of HNUMSC-C language

uart_spi练习

pytorch相关知识点总结
随机推荐
php过滤特殊字符(仅保留中文、字母、数字、下划线)
显著性检验--学习笔记
eladmin容器部署超详细过程
How js implements array deduplication (7 kinds)
ApiFile配置环境
MT4/MQL4入门到精通EA课程第二课-常用的功能函数
Maya engine modeling
The security of the pension insurance?Reliable?
炫酷-轮播图-走马灯
HNUMSC-C语言第一课
2022杭电多校第五场1007(生成函数+启发式合并+ntt)
点击div内部默认文本被选中
Etcd realize large-scale application service management of actual combat
JS 实现千分位分隔符
Duplicate class com.google.common.util.concurrent.ListenableFuture found in modules
力扣刷题记录1.5-----367. 有效的完全平方数
C#计算两个时间相差多少天、时、分、秒
快速乘写法
10.1-----19. Delete the Nth node from the bottom of the linked list
终于有人把灰度发布架构设计讲明白了