Ugly Numbers
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 21920   Accepted: 9789

Description

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, ...

shows the first 10 ugly numbers. By convention, 1 is included.

Given the integer n,write a program to find and print the n'th ugly number.

Input

Each line of the input contains a postisive integer n (n <= 1500).Input is terminated by a line with n=0.

Output

For each line, output the n’th ugly number .:Don’t deal with the line with n=0.

Sample Input

1
2
9
0

Sample Output

1
2
10

Source

 
用set 自动去除重复元素的性质,也可以用一个优先队列保存所有已生成的丑数,每次取出最小的丑数,生产3个新的丑数,因为丑数有多种生成方式所以每次要判断一个丑数是否已经生成过了
set代码:
 #include<cstdio>
#include<set>
#include<queue>
using namespace std;
#define ll long long
#define N 1510
set<ll> s;
set<ll> :: iterator it;
ll un[N];
int main()
{
int n, c = ;
s.clear();
s.insert();
it = s.begin();
while(c<)
{
ll u = *it;
un[c++] = u;
s.insert(*u), s.insert(*u), s.insert(*u);
it++;
}
// for(int i = 0; i < 20; i++) printf("%lld ", un[i]); puts("");
while(~scanf("%d",&n),n)
printf("%lld\n",un[n-]);
return ;
}

用到优先队列的方法:

 #include<cstdio>
#include<set>
#include<queue>
using namespace std;
#define ll long long
#define N 1510
set<ll> s;
priority_queue<ll, vector<ll> ,greater<ll> > qu;
ll un[N];
int main()
{
int n;
s.insert();
qu.push();
for(int i = ; i < ; i++)
{
ll tm = qu.top();
if(!s.count(tm*)) { s.insert(tm*) ; qu.push(tm*);}
if(!s.count(tm*)) { s.insert(tm*) ; qu.push(tm*);}
if(!s.count(tm*)) { s.insert(tm*) ; qu.push(tm*);}
qu.pop();
}
set<ll>::iterator it;
int cnt = ;
for( it = s.begin() ; cnt < ; it++)//结束条件改一下 要是it<s.end() 会runtime error
{
un[cnt++] = (*it);
}
// for(int i = 0; i < 1501; i++) printf("%lld\n", un[i]);
while(~scanf("%d",&n),n) {
printf("%lld\n",un[n-]);
}
return ;
}

注意,结束条件是pop了1500次,不是s的size是1500,因为最后还会进来更小的值

Ugly Numbers(STL应用)的更多相关文章

  1. 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 ...

  2. UVA.136 Ugly Numbers (优先队列)

    UVA.136 Ugly Numbers (优先队列) 题意分析 如果一个数字是2,3,5的倍数,那么他就叫做丑数,规定1也是丑数,现在求解第1500个丑数是多少. 既然某数字2,3,5倍均是丑数,且 ...

  3. UVa136 Ugly Numbers(优先队列priority_queue)

    Ugly Numbers 题目 Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, ...

  4. [POJ1338]Ugly Numbers

    [POJ1338]Ugly Numbers 试题描述 Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequ ...

  5. Ugly Numbers

    Ugly Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21918 Accepted: 9788 Descrip ...

  6. poj 1338 Ugly Numbers(丑数模拟)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063? viewmode=contents 题目链接:id=1338&q ...

  7. leetcode@ [263/264] Ugly Numbers & Ugly Number II

    https://leetcode.com/problems/ugly-number/ Write a program to check whether a given number is an ugl ...

  8. Geeks Interview Question: Ugly Numbers

    Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence1, 2, 3, 4, 5, 6, 8, 9, ...

  9. 136 - Ugly Numbers

     Ugly Numbers  Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3 ...

随机推荐

  1. Ubuntu14.04下安装 boost (boost_1.54 最简单的方法)

    直接执行命令: sudo apt-get  install libboost-dev 测试: 创建一个 boost_test.cpp 文件,写入 #include<iostream> #i ...

  2. ArcGIS API for JavaScript 4.3 与ArcGIS Server联动使用【地图服务】

    [前言] 有好些网友问我怎么使用Server发布的地图服务了,其实非常的简单. 我在这里先声明:不提供Server软件,需要的请自行使用互联网搜索资源: 不阐述Server如何发布各各种服务,但是我会 ...

  3. 视觉SLAM的数学表达

    相机是在某些时刻采集数据的,所以只关心这些时刻的位置和地图. 就把这一段时间的运动变成了李三时刻 t=1,2,...K当中发生的事情. 在这些事可,x表示机器自身的位置. x1,x2,x3,x4... ...

  4. Python函数篇(6)-常用模块及简单的案列

    1.模块   函数的优点之一,就是可以使用函数将代码块与主程序分离,通过给函数指定一个描述性的名称,并将函数存储在被称为模块的独立文件中,再将模块导入主程序中,通过import语句允许在当前运行的程序 ...

  5. 2、转载一篇,浅析人脸检测之Haar分类器方法

    转载地址http://www.cnblogs.com/ello/archive/2012/04/28/2475419.html 浅析人脸检测之Haar分类器方法  [补充] 这是我时隔差不多两年后, ...

  6. a 标签的四种样式

    在支持 CSS 的浏览器中,链接的不同状态都可以不同的方式显示,这些状态包括:活动状态,已被访问状态,未被访问状态,和鼠标悬停状态.分别如下设置: a:link {color: red} /* 未访问 ...

  7. 关于java 定时任务

    几种任务调度的 Java 实现方法与比较 综观目前的 Web 应用,多数应用都具备任务调度的功能.本文由浅入深介绍了几种任务调度的 Java 实现方法,包括 Timer,Scheduler, Quar ...

  8. js拖拽的封装

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  9. asp.net mvc ajax提交模型到控制器

    http://blog.csdn.net/loongsking/article/details/53224473 function btn_submit() {        var data = n ...

  10. 执行查询“BACKUP LOG [XXX] TO DISK = N'F:\\BackData\\事务日至备份\\...”失败,错误如下:“无法执行 BACKUP LOG,因为当前没有数据库备份。 BACKUP LOG 正在异常终止。

    执行查询"BACKUP LOG [XXX] TO  DISK = N'F:\\BackData\\事务日至备份\\..."失败,错误如下:"无法执行 BACKUP LOG ...