UVA.136 Ugly Numbers (优先队列)
UVA.136 Ugly Numbers (优先队列)
题意分析
如果一个数字是2,3,5的倍数,那么他就叫做丑数,规定1也是丑数,现在求解第1500个丑数是多少。
既然某数字2,3,5倍均是丑数,且1为丑数,那么不妨从1开始算起。算完之后2,3,5均为丑数,然后再从2算起,4,5,10均为丑数……直到算到第1500个即可。那么有如下的问题:
如何从算出来的丑数中取出最小的?
如何保证没有计算重复的丑数?
对于第一个问题,可以采用优先队列的方法,即有序的队列,且为升序,每次只需要取出队首元素即可。
第二个问题,可以采用STL中的set容器即可,如果发现有重复的,即count != 0 那么说明这个数字已经算出过了,就不让其加入队列即可。
代码总览
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#define nmax
using namespace std;
typedef long long LL;
const int init []= {2,3,5};
int main()
{
set<LL>s;
priority_queue<LL,vector<LL>,greater<LL> > pq;
s.insert(1);
pq.push(1);
LL x;
for(int i =1; ;++i){
x = pq.top(); pq.pop();
if(i == 1500) break;
for(int j = 0; j<3; ++j){
LL t = x * init[j];
if(s.count(t) == 0){pq.push(t); s.insert(t);}
}
}
printf("The 1500'th ugly number is %lld.\n",x);
return 0;
}
UVA.136 Ugly Numbers (优先队列)的更多相关文章
- 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 ...
- 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以外的素数整除的的数称为丑数. 和杭电的那一题丑数一样--这里学的紫书上的用优先队列来做. 用已知的丑数去生成新的丑数,利用优先队列的能够每次取出当前最小的丑数 ...
- UVa 136 - Ugly Numbers
题目大意:只有素因子2,3,5的数叫做丑数.输出第1500个丑数即可. 这个...好吧,直接输出就是了.自己写一个小程序先计算一下,这就是黑盒测试的好处啊,“我们的目标是解决问题,而不是为了写程序而写 ...
- 136 - Ugly Numbers
Ugly Numbers Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3 ...
- 136 Ugly Numbers(priority_queue+逆向求解要求数)
题目链接: https://cn.vjudge.net/problem/UVA-136 /*问题 输出第1500个丑数,丑数的定义是不能被2,3,5以外的其他素数整除的数 解题思路 直接硬暴力先试一下 ...
- Ugly Numbers UVA - 136(优先队列+vector)
Problem Description Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, ...
- 丑数(Ugly Numbers, UVa 136)
丑数(Ugly Numbers, UVa 136) 题目描述 我们把只包含因子2.3和5的数称作丑数(Ugly Number).求按从小到大的顺序的第1500个丑数.例如6.8都是丑数,但14不是,因 ...
- 【UVA - 136】Ugly Numbers(set)
Ugly Numbers Descriptions: Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequ ...
随机推荐
- Qt-QML-Repeater-导航条
上篇文章中,我写了一个自己的Button,也就是美化了一下QML自带的Button 就是上面的这个,剩下的就是放三张图片在上面就可以了,当然了,这个Button在后期,还是会加入更让多的美化,比如,可 ...
- lesson 24 A skeleton in the cupboard
lesson 24 A skeleton in the cupboard conceal sth from sb 对某人隐藏某事 He conceals his girlfriend from his ...
- Maya Api笔记 - How polygons are handled internally
为加深记忆和理解Maya的Polygon,尝试利用空闲时间翻译Maya Api文档相关章节. How polygons are handled internally - 多边形是如何在内部处理的
- centos7安装zabbix3.2详解
服务器端安装 1.安装仓库 rpm -ivh http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noar ...
- 四、oracle 用户管理二
一.使用profile管理用户口令概述:profile是口令限制,资源限制的命令集合,当建立数据库时,oracle会自动建立名称为default的profile.当建立用户没有指定profile选项时 ...
- Windows下PATH等环境变量详解(转载)
本文转载自http://legend2011.blog.51cto.com/3018495/553255 在学习JAVA的过程中,涉及到多个环境变量(environment variable)的概念, ...
- HDU 4115 Eliminate the Conflict(2-SAT)(2011 Asia ChengDu Regional Contest)
Problem Description Conflicts are everywhere in the world, from the young to the elderly, from famil ...
- @ModelAttribute使用详解
1.@ModelAttribute注释方法 例子(1),(2),(3)类似,被@ModelAttribute注释的方法会在此controller每个方法执行前被执行,因此对于一个control ...
- 深入理解Java对象序列化(转载)
原文地址:http://developer.51cto.com/art/201202/317181.htm 1. 什么是Java对象序列化 Java平台允许我们在内存中创建可复用的Java对象,但一般 ...
- lintcode-28-搜索二维矩阵
搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每行的第一个数大于上一行的最后一个整数. 样例 考虑下列矩阵: [ [1, 3, ...