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 ...
随机推荐
- 编译Chromium出现warning C4819的解决办法
编译Chromium时出现 warning C4819: The file contains a character that cannot be represented in the current ...
- 「题目代码」P1039~P1043(Java)
P1039 谭浩强C语言(第三版)习题4.9 import java.util.*; import java.io.*; import java.math.BigInteger; public cla ...
- Linux命令应用大词典-第12章 程序编译
12.1 gcc:GNU项目的C和C++编译器 12.2 gdberver:为GNU调试的远程服务器 12.3 cmake:跨平台的Makefile生成工具 12.4 indent:更改通过插入或删除 ...
- 【Extremely Basic Words for Listening】word list
[Extremely Basic Words for Listening]word list updated continuously recite count: 0 careless exercis ...
- [wirtting] top01 independent
Do you agree or disagree with the following statement? At universities and colleges, sports and soci ...
- [ Continuously Update ] The Paper List of Image / Video Captioning
Papers Published in 2018 Convolutional Image Captioning - Jyoti Aneja et al., CVPR 2018 - [ Paper Re ...
- POJ 1639 Picnic Planning(最小度限制生成树)
Description The Contortion Brothers are a famous set of circus clowns, known worldwide for their inc ...
- Alpha冲刺——第一天
Alpha第一天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...
- python学习笔记03:python的核心数据类型
从根本上讲,Python是一种面向对象的语言.它的类模块支持多态,操作符重载和多重继承等高级概念,并且以Python特有的简洁的语法和类型,OOP十分易于使用.Python的语法简单,容易上手. Py ...
- iOS-【UIDynamic-UIKit动力学】
如果看不到图片 可以尝试更换浏览器(推荐Safari ) 0.了解 •Dynamic Animator:动画者,为动力学元素提供物理学相关的能力及动画,同时为这些元素提供相关的上下文,是动力学元素与底 ...