Ugly Numbers(STL应用)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 21920 | Accepted: 9789 |
Description
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
Output
Sample Input
1
2
9
0
Sample Output
1
2
10
Source
#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应用)的更多相关文章
- 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 (优先队列)
UVA.136 Ugly Numbers (优先队列) 题意分析 如果一个数字是2,3,5的倍数,那么他就叫做丑数,规定1也是丑数,现在求解第1500个丑数是多少. 既然某数字2,3,5倍均是丑数,且 ...
- UVa136 Ugly Numbers(优先队列priority_queue)
Ugly Numbers 题目 Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, ...
- [POJ1338]Ugly Numbers
[POJ1338]Ugly Numbers 试题描述 Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequ ...
- Ugly Numbers
Ugly Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21918 Accepted: 9788 Descrip ...
- poj 1338 Ugly Numbers(丑数模拟)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063? viewmode=contents 题目链接:id=1338&q ...
- 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 ...
- 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, ...
- 136 - Ugly Numbers
Ugly Numbers Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3 ...
随机推荐
- 【Netty】源码分析目录
前言 为方便系统的学习Netty,特整理文章目录如下. [Netty]第一个Netty应用 [Netty]Netty核心组件介绍 [Netty]Netty传输 [Netty]Netty之ByteBuf ...
- hibernate核心API使用
1.Configuration 加载核心配置文件,核心配置文件名称和位置固定,否则会找不到 2.SessionFactory对象一个项目只创建一个,大家公用 根据配置文件和映射关系创建表 由于要创建表 ...
- openstack ocata版本简化安装
Network Time Protocol (NTP) Controller Node apt install chrony Edit the /etc/chrony/chrony.conf 添加如下 ...
- iOS学习——iOS常用的存储方式
不管是在iOS还是Android开发过程中,我们都经常性地需要存储一些状态和数据,比如用户对于App的相关设置.需要在本地缓存的数据等等.根据要存储的的数据的大小.存储性质以及存储类型,在iOS和An ...
- 关于git的一些理论知识
一.什么是版本控制器 好多刚用git的coder一说起git,就随口会说出版本控制器嘛,我问那是干嘛的,大部分人就回答上传代码的.然后会用,但是有些理论你问他们他们就不知道了,比如不是代码的文件就不能 ...
- K:java中枚举的常见用法
用法一:常量 在JDK1.5 之前,我们定义常量都是: public static fianl.....现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法. ...
- 程序包管理rpm、yum与简单编译安装程序
Linux程序包管理 Linux中软件的安装主要有两种形式:一种是直接下载源代码包自行编译后安装,另一种直接获取rpm软件包进行安装. 程序的组成部分: 二进制程序:程序的主体文件,比如我们运行一个l ...
- JQuery基本语法(部分)
1.jQuery介绍 jQuery 是一个 JavaScript 函数库. jQuery 库包含以下特性: HTML 元素选取 HTML 元素操作 CSS 操作 HTML 事件函数 JavaScrip ...
- linux系统编程:自己动手写一个who命令
who命令的作用用于显示当前有哪些用户登录到系统. 这个命令执行的原理是读取了系统上utmp文件中记录的所有登录信息,直接显示出来的 utmp文件在哪里呢? man who的时候,在手册下面有这么一段 ...
- css中的 nav ul li ul 与 nav>ul>li 这两种写法的区别
<nav> <ul id="a"> <li>a1</li> <li>a2</li> <li>a3 ...