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. mobile开发技巧(转)

    1.隐藏地址栏 很多文档介绍通过调用 window.scrollTo(0, 1); 就可以隐藏地址栏,但是通过实践发现隐藏地址栏还是真够坑爹的啊,只调用这一句话一般不会起作用,我们需要 functio ...

  2. Nginx+Keepalived配置

    1. Nginx安装 (1) 环境:分别在2台服务器上部署nginx且步骤一致: 如192.138.86.1和192.138.86.2 (2) 下载官网最新稳定版,地址:https://nginx.o ...

  3. bzoj 4771: 七彩树

    Description 给定一棵n个点的有根树,编号依次为1到n,其中1号点是根节点.每个节点都被染上了某一种颜色,其中第i个节 点的颜色为c[i].如果c[i]=c[j],那么我们认为点i和点j拥有 ...

  4. bzoj 5084: hashit

    Description 你有一个字符串S,一开始为空串,要求支持两种操作 在S后面加入字母C 删除S最后一个字母 问每次操作后S有多少个两两不同的连续子串 Solution 先忽略删除操作,建出最终的 ...

  5. 解决The current branch is not configured for pull No value for key branch.master.merge found in config

    使用Git Pull项目的时候出现这个问题: The current branch is not configured for pull No value for key branch.master. ...

  6. c#-IO和序列化操作

    IO 用到的命名空间:using System.IO; 文件和目录的管理! File类 FileInfo类 Directory类 DirectoryInfo类 操作文件的类! FileStream{ ...

  7. SpringSecurity 3.2入门(1)框架介绍

    关于Spring Security Spring Security,这是一种基于Spring AOP和Servlet过滤器 [7] 的安全框架.它提供全面的安全性解决方案,同时在 Web 请求级和方法 ...

  8. jsp的动作标签

    常用的标签: 1. forward   请求转发 [基本不使用] <==> request.getRequestDispatcher(url).forward(request,respon ...

  9. CMS API Overview - 翻译

    source: http://activemq.apache.org/cms/cms-api-overview.html 1. CMS是啥? C++版本的API,用于收发消息(JMS). 如果您已熟悉 ...

  10. 编程进阶:Java小白的序列化Serializable接口

    在之前的学习过程中,我们知道了如何使用FileInputStream输入流和FileOutputStream输出流编写程序读写文件. 下面我们来学习一下如何使用序列化和反序列化读写文件. 一.序列化 ...