Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ...

shows the first 11 ugly numbers. By convention, 1 is included. Write a program to find and print the 1500’th ugly number.

Input There is no input to this program.

Output Output should consist of a single line as shown below, with ‘<number>’

replaced by the number computed. Sample Output The 1500'th ugly number is <number>.

分析:

第一种方法:遍历每个数,判断是否为ugly,直到第1500个ugly为止(简单粗暴,没有效率可言,runtime 19s)

#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
bool isugly(int n)
{
while(n>)
{
if(n%==)
n/=;
else if(n%==)
n/=;
else if(n%==)
n/=;
else
return ;
}
return ;
}
int main()
{
int cnt=;
int num=;
while(cnt<)
{
if(isugly(num))
cnt++;
num++;
}
cout<<num-<<endl;
return ;
}

第二种方法:利用STL优先队列从小到大生成各个ugly number。最小的丑数是1,而对于任意丑数x,2x,3x和5x也都是丑数。注意丑数判重。

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<set>
using namespace std;
typedef long long LL;
int f[]={,,};
int main()
{
priority_queue<LL,vector<LL>,greater<LL> > qq;
set<LL> s;
qq.push();
s.insert();
for(int i=;;i++)
{
LL x=qq.top();
qq.pop(); if(i==)
{
cout << "The 1500'th ugly number is " << x << ".\n";
break;
}
for(int j=;j<;j++)
{
LL x2=x*f[j];
if(!s.count(x2))
{
s.insert(x2);
qq.push(x2);
}
}
}
// cout<<"The 1500'th ugly number is 859963392."<<endl;
return ;
}

UVA - 136 Ugly Numbers(丑数,STL优先队列+set)的更多相关文章

  1. UVA.136 Ugly Numbers (优先队列)

    UVA.136 Ugly Numbers (优先队列) 题意分析 如果一个数字是2,3,5的倍数,那么他就叫做丑数,规定1也是丑数,现在求解第1500个丑数是多少. 既然某数字2,3,5倍均是丑数,且 ...

  2. UVa 136 Ugly Numbers【优先队列】

    题意:给出丑数的定义,不能被除2,3,5以外的素数整除的的数称为丑数. 和杭电的那一题丑数一样--这里学的紫书上的用优先队列来做. 用已知的丑数去生成新的丑数,利用优先队列的能够每次取出当前最小的丑数 ...

  3. UVA - 136 Ugly Numbers (有关set使用的一道题)

    Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence1, 2, 3, 4, 5, 6, 8, 9, ...

  4. UVa 136 - Ugly Numbers

    题目大意:只有素因子2,3,5的数叫做丑数.输出第1500个丑数即可. 这个...好吧,直接输出就是了.自己写一个小程序先计算一下,这就是黑盒测试的好处啊,“我们的目标是解决问题,而不是为了写程序而写 ...

  5. lintcode :Ugly Numbers 丑数

    题目 丑数 设计一个算法,找出只含素因子3,5,7 的第 k 大的数. 符合条件的数如:3,5,7,9,15...... 样例 如果k=4, 返回 9 挑战 要求时间复杂度为O(nlogn)或者O(n ...

  6. 136 Ugly Numbers(priority_queue+逆向求解要求数)

    题目链接: https://cn.vjudge.net/problem/UVA-136 /*问题 输出第1500个丑数,丑数的定义是不能被2,3,5以外的其他素数整除的数 解题思路 直接硬暴力先试一下 ...

  7. Humble Numbers(丑数) 超详解!

    给定一个素数集合 S = { p[1],p[2],...,p[k] },大于 1 且素因子都属于 S 的数我们成为丑数(Humble Numbers or Ugly Numbers),记第 n 大的丑 ...

  8. hdu1058丑数(优先队列、暴力打表)

    hdu1058 题意:当一个数只有2.3.5.7这四种质因数时(也可以一种都没有或只有其中几种),这个数就是丑数,输出第 n 个丑数是多少: 其实并没有发现hdu把这道题放在 dp 专题里的意图,我的 ...

  9. Ugly number丑数2,超级丑数

    [抄题]: [思维问题]: [一句话思路]:Long.valueOf(2)转换为long型再做 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图 ...

随机推荐

  1. Kubernetes系列:(1) 初探

    1. 背景 在部门内容组织了一次K8s的培训,普及了下K8s的概念.框架.操作等,为便于后期查阅,也为了进一步深究K8s,因此开展K8s系列,周期不定- 2. 概念 (1) 含义:来自希腊语,意为&q ...

  2. 使用 GitHub API 进行数据分析 (Node.js)

    使用 GitHub API 进行数据分析 (Node.js) Node.js 的访问 GitHub 的 API 库,通过 npm 或者 yarn 安装: yarn add github-api 官方示 ...

  3. 使用Git(msysgit)和TortoiseGit上传代码到GitHub

    1.准备 下载Git for Windows (msysgit) 下载TortoiseGit 安装过程很简单,一直点击下一步到完成即可. 2.配置TortoiseGit 1.双击TortoiseGit ...

  4. js【jquery】 - DOM操作

    1.修改元素样式 js方式: var div2 = document.getElementById("") div2.style.width = '200px'; div2.cla ...

  5. [SQL SERVER系列]工作经常使用的SQL整理,实战篇(一)[原创]

    工作经常使用的SQL整理,实战篇,地址一览: 工作经常使用的SQL整理,实战篇(一) 工作经常使用的SQL整理,实战篇(二) 工作经常使用的SQL整理,实战篇(三) 目录概览: 1.数据库 2.表 3 ...

  6. rest-framework框架——APIView和序列化组件

    一.快速实例 Quickstart http://www.cnblogs.com/yuanchenqi/articles/8719520.html restful协议 ---- 一切皆是资源,操作只是 ...

  7. HTML <frameset> 标签

    <frameset></frameset>:框架标签,可以将页面分割,被frameset标签分割的页面,不允许使用body标签;frameset标签页面内只能出现framese ...

  8. Web服务器父与子 Apache和Tomcat

    Apache是世界使用排名第一的Web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一.在Apache基金会里面Apache S ...

  9. show与ShowDialog substring

    substring public String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串.该子字符串从指定的 beg ...

  10. H5手机页面剖析

    <!--强制使用webkit内核进行渲染--><meta http-equiv="X-UA-COMPATIBLE" content="IE=edge, ...