UVA - 136 Ugly Numbers(丑数,STL优先队列+set)
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)的更多相关文章
- UVA.136 Ugly Numbers (优先队列)
UVA.136 Ugly Numbers (优先队列) 题意分析 如果一个数字是2,3,5的倍数,那么他就叫做丑数,规定1也是丑数,现在求解第1500个丑数是多少. 既然某数字2,3,5倍均是丑数,且 ...
- UVa 136 Ugly Numbers【优先队列】
题意:给出丑数的定义,不能被除2,3,5以外的素数整除的的数称为丑数. 和杭电的那一题丑数一样--这里学的紫书上的用优先队列来做. 用已知的丑数去生成新的丑数,利用优先队列的能够每次取出当前最小的丑数 ...
- 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, ...
- UVa 136 - Ugly Numbers
题目大意:只有素因子2,3,5的数叫做丑数.输出第1500个丑数即可. 这个...好吧,直接输出就是了.自己写一个小程序先计算一下,这就是黑盒测试的好处啊,“我们的目标是解决问题,而不是为了写程序而写 ...
- lintcode :Ugly Numbers 丑数
题目 丑数 设计一个算法,找出只含素因子3,5,7 的第 k 大的数. 符合条件的数如:3,5,7,9,15...... 样例 如果k=4, 返回 9 挑战 要求时间复杂度为O(nlogn)或者O(n ...
- 136 Ugly Numbers(priority_queue+逆向求解要求数)
题目链接: https://cn.vjudge.net/problem/UVA-136 /*问题 输出第1500个丑数,丑数的定义是不能被2,3,5以外的其他素数整除的数 解题思路 直接硬暴力先试一下 ...
- Humble Numbers(丑数) 超详解!
给定一个素数集合 S = { p[1],p[2],...,p[k] },大于 1 且素因子都属于 S 的数我们成为丑数(Humble Numbers or Ugly Numbers),记第 n 大的丑 ...
- hdu1058丑数(优先队列、暴力打表)
hdu1058 题意:当一个数只有2.3.5.7这四种质因数时(也可以一种都没有或只有其中几种),这个数就是丑数,输出第 n 个丑数是多少: 其实并没有发现hdu把这道题放在 dp 专题里的意图,我的 ...
- Ugly number丑数2,超级丑数
[抄题]: [思维问题]: [一句话思路]:Long.valueOf(2)转换为long型再做 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图 ...
随机推荐
- console命令详解:(转载学习)
Console命令详解,让调试js代码变得更简单 Firebug是网页开发的利器,能够极大地提升工作效率. 但是,它不太容易上手.我曾经翻译过一篇<Firebug入门指南>,介绍了一些 ...
- unity烘焙记录
1.Unity Android 阴影不显示.阴影显示不正确 解决 http://blog.csdn.net/xuetao0605/article/details/50626181 2.阴影强度问题 不 ...
- C++程序设计基础(1)程序的编译和执行
注:读<程序员面试笔记>笔记总结 1.编译执行过程 1.1预处理: (1)所有以#开头的代码都属于预处理的范畴:#include,#define,#ifdef(#ifndef,#endif ...
- [转]ASP.NET Core / MVC 6 HttpContext.Current
本文转自:http://www.spaprogrammer.com/2015/07/mvc-6-httpcontextcurrent.html As you know with MVC 6, Http ...
- 获取httpservletrequest所有参数的名称和值
1.方法 private Map showParams(HttpServletRequest request) { Map map = new HashMap(); Enumeration param ...
- 在 Linux 上创建第一个 Service Fabric Java 应用程序
先决条件 开始之前,请安装 Service Fabric SDK.Azure CLI,并在 Linux 开发环境中设置开发群集. 如果使用 Mac OS X,则可使用 Vagrant 在虚拟机中设置 ...
- (二)HTML中的部分标签
HTML作为一种超文本标记语言,其中用到了大量的标签,昨天主要看了HTML中的图像标签和表格标签. (一)图像标签 <img> <img src="url"/&g ...
- Jedis Cluster源码分析
最近一个项目用到Jedis客户端,需要对这个客户端进行改造.看了一下Jedis Cluster源码,做个记录 首先,说核心内容, 在Jedis源码中,关于cluster有个两个重要的map.一个是no ...
- 2、Spring之AOP
AOP术语 通知:定义了切面是什么以及何时使用.除了要描述页面要完成的工作,通知还解决了何时执行这个工作的问题. 连接点:连接点是一个物理的存在.这个点可以是调用方法时.抛出异常时.甚至是修改一个字段 ...
- C#程序的编译和执行
1.在讲解 C# 程序的编译与执行之前,首先了解以下两个概念,以便充分理解C# 程序的运行. CLI--Common Language Infrastructure 的简称,C# 程序在Microso ...