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. 将自己的域名代理到Gitpages

    相信有很多程序员都有自己的域名,甚至很多人还有自己的服务器.去年我也买了半年的阿里云,在tomcat里面发war包,相当于一个正式的项目.但是很多前端程序员应该要求很简单,就是能将静态的html发布就 ...

  2. lua的通用print函数

    1.前言 最近在做关于openresty方面的工作,涉及到lua脚本语言,经常需要打日志查看内容.普通的print函数遇到nil或table时,非常无力.而项目中的代码经常遇到参数为nil或table ...

  3. Java 银行家算法

    实验存档,代码特别烂.. 测试.java package operating.test; import operating.entity.bank.Bank; import operating.ent ...

  4. su 和 sudo 命令的区别-转载

    link 一. 使用 su 命令临时切换用户身份                   1.su 的适用条件和威力   su命令就是切换用户的工具,怎么理解呢?比如我们以普通用户beinan登录的,但要 ...

  5. & 与 kill -3

    mysqld_safe --defaults-file=/app/3307/my.cnf 2>&1 1>/dev/null & 将mysqld服务进程放入后台后, 因为忘记 ...

  6. 关于python中的dir函数

    dir函数用于查看python对象的属性,如果所查看的python对象已经定义了__dir__方法,则使用dir会返回定义的__dir__方法的返回值.如果没有定义__dir__方法,则会从__dic ...

  7. 初步了解 Django Models

    Part1:了解主键 1.      Python和Django版本如下: E:\django>python3 -V Python 3.5.2 E:\django>python3 -m d ...

  8. ssh整合开发

    ssh整合思想 ssh整合 第一步:导入ssh相关jar包 第二步:搭建struts环境   (1)创建 action  struts.xml配置文件, 配置action struts.xml约束 & ...

  9. Go Language 开发环境搭建

    [前言] Go语言是Google公司2009年发布的新一代的开发语言,其最大的亮点是原生自带并发,在合适的场合使用合适的代码,我们的系统将会更加完美.下面我们进行go语言的安装和配置. 一.下载安装 ...

  10. c/c++ console(控制台)编程详解

    前言: 控制台文本窗口的控制是基于win32 api(win32 api可理解为微软给我们提供的一系列函数的集合)实现的; 例1: #include <stdio.h>int main() ...