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型再做 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图 ...
随机推荐
- C# Uploadify 文件上传组件的使用
一.页面的构建 1.要引用的JS和CSS <link href="../css/jquery-ui.css" rel="stylesheet" type= ...
- SQL脚本整理系列 三
触发器 SQL 2008 怎么实现删除学生表里面的一条记录,成绩表里面关于这个学生的记录也同时删掉,谢求具体代码 --创建表 DROP TABLE tstudent GO CREATE TABLE t ...
- 优化代码CPU层面
今天在看<支撑处理器的技术>,其中,讲到了CPU流水线.在指令之间,如果下一条指令,需要用到上一条指令的结果,会影响到流水线的执行.书上给出了几种解决方案,一个是在指令中间插入一下无关的指 ...
- 自己动手实现STL 01:内存配置器的实现(stl_alloc.h)
一.前言 在STL中,容器是其中的重中之重,基本的STL中的算法,仿函数等都是围绕着容器实现的功能.而,内存配置器,是容器的实现的基础.所以,我第一次要去编写便是内存配置器的实现.在STL中,内存配置 ...
- Hibernate中的一对一注解配置
Card类 package cn.OneToOne2017109.entity; import javax.persistence.*; /** * Created by YSS on 2017/10 ...
- SQL:Example Uses of the SUBSTRING String Function
---Example Uses of the SUBSTRING String Function --http://www.sql-server-helper.com/tips/tip-of-the- ...
- iDempiere 使用指南 绿色版一键启动测试环境
Created by 蓝色布鲁斯,QQ32876341,blog http://www.cnblogs.com/zzyan/ iDempiere官方中文wiki主页 http://wiki.idemp ...
- wxpython wx.windows的API
wx.Window is the base class for all windows and represents any visible object on screen. All control ...
- 深度搜索C语言伪代码
bool DFS(Node n, int d){ if (d == 4){//路径长度为返回true,表示此次搜索有解 return true; } for (Node nextNode in n){ ...
- 设计模式之装饰模式(Decorator)
1. 装饰者模式,动态地将责任附加到对象上.若要扩展功能,装饰者提供了比继承更加有弹性的替代方案. 2.组合和继承的区别 继承.继承是给一个类添加行为的比较有效的途径.通过使用继承,可以使得子类在拥有 ...