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. React:入门计数器

    ---恢复内容开始--- 把React的官网入门例子全看一遍,理解了,但自己从头开始写有点困难,这次强迫自己从头开始写,并写好注释: import React, { Component } from ...

  2. php中static 静态关键字

    一直依赖对于php中static关键字比较模糊,只是在单例模式中用过几次.上网查了查,没有找到很全的介绍,自己总结一下. 根据使用位置分为两部分 1.函数体中的静态变量 2.类中的静态属性和方法 1 ...

  3. C++ 头文件系列(stdexcept)

    预定义异常类 这个头文件包含的内容非常简单,只包含9个异常类,均从exception类派生,因此我们用三张图来描述: 这里仅解释两点: overflow : 指值的大小超过 整型 变量能表示的范围,即 ...

  4. Docker(四):Docker基本网络配置

    1.Libnetwork Libnetwork提出了新的容器网络模型简称为CNM,定义了标准的API用于为容器配置网络. CNM三个重要概念: 沙盒:一个隔离的网络运行环境,保存了容器网络栈的配置,包 ...

  5. 深入学习rollup来进行打包

    深入学习rollup来进行打包 阅读目录 一:什么是Rollup? 二:如何使用Rollup来处理并打包JS文件? 三:设置Babel来使旧浏览器也支持ES6的代码 四:添加一个debug包来记录日志 ...

  6. java 修饰符之修饰范围

    不同修饰符有不同修饰范围,为了对修饰符有更明确的认识,使用表格总结. 抽象\关键字 public protected private static final abstract default 类 √ ...

  7. [摘抄]VC6.0移植到VS2008(vs2005)后的错误总结(未全部验证)

    ============================================================================================= 201405 ...

  8. Jmeter非GUI模式运行

    非GUI模式,即命令行模式,运行 JMeter 测试脚本能够大大缩减所需要的系统资源. 使用的命令: jmeter  -n  -t  脚本文件路径   -l   结果输出文件路径   -j   日志文 ...

  9. 关于 const 的一点小研究

    在饱受 var 的折磨之后,ES6 终于推出了新的定义变量的方法:let 和 const 和 var 相比,let 和 const 有了自己的作用域,let 用于定义变量,而 const 用于定义常量 ...

  10. DBCC命令

    DBCC SQLMGRSTATS 用于产生3个不同的值,这些值用在你想查看高速缓存在ad-hoc和预编译的TSQL语句中是如何工作的 Memory Used(8K Pages):若内存页的数量非常大, ...