poj 1338 ugly numbers
来源:互联网 发布:网络节点 游戏 编辑:程序博客网 时间:2024/05/21 09:57
Ugly Numbers
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 20249 Accepted: 9003
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.
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
1290
Sample Output
1210
Source
New Zealand 1990 Division I,UVA 136
优先队列和set的妙用
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<set>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
int main()
{
int n;
int c[3]={2,3,5};
while(scanf("%d",&n)&&n)
{
priority_queue<LL,vector<LL>,greater<LL> > pq;
set<LL> s;
pq.push(1);
s.insert(1);
for(int i=1;;i++)
{
LL x=pq.top();pq.pop();
if(i==n)
{
printf("%lld\n",x);
break;
}
for(int j=0;j<3;j++)
if(!s.count(c[j]*x))
{
s.insert(c[j]*x);
pq.push(c[j]*x);
}
}
}
}
#include <cstring>
#include <iostream>
#include <algorithm>
#include<set>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
int main()
{
int n;
int c[3]={2,3,5};
while(scanf("%d",&n)&&n)
{
priority_queue<LL,vector<LL>,greater<LL> > pq;
set<LL> s;
pq.push(1);
s.insert(1);
for(int i=1;;i++)
{
LL x=pq.top();pq.pop();
if(i==n)
{
printf("%lld\n",x);
break;
}
for(int j=0;j<3;j++)
if(!s.count(c[j]*x))
{
s.insert(c[j]*x);
pq.push(c[j]*x);
}
}
}
}
0 0
- POJ 1338 Ugly Numbers
- POJ 1338 Ugly Numbers
- POJ 1338 ugly numbers
- poj 1338 Ugly Numbers
- POJ 1338 Ugly Numbers
- POJ-1338-Ugly Numbers
- POJ 1338 Ugly Numbers
- poj-1338Ugly Numbers
- POJ 1338 - Ugly Numbers
- POJ 1338 Ugly Numbers
- poj 1338 ugly numbers
- Ugly Numbers(poj 1338)
- poj 1338 Ugly Numbers
- poj-1338-Ugly Numbers
- poj 1338 Ugly Numbers
- poj-1338-Ugly Numbers
- poj-1338-Ugly Numbers
- poj 1338 Ugly Numbers
- Extjs4之动态添加FormPanel
- SOP 封装
- 学校新机房装系统——联想机房网络同传
- git 使用汇总
- JS 控制文本框只能输入中文、英文、数字与指定特殊符号
- poj 1338 ugly numbers
- 实现Nginx中使用PHP-FPM时记录PHP错误日志的配置方法
- Install Skype 4.0.0.8 in Ubuntu 12.04
- 二维,三维树状数组模板
- Mini2440 X35 ADC触摸转换
- openstack windows下远程debug调试(pycharm)
- Java设计模式之享元模式
- 【JAVA】关于父子类转化的问题
- testNG--testNG installation