丑数 简单的优先队列和set
来源:互联网 发布:软件系统集成设计方案 编辑:程序博客网 时间:2024/05/01 02:09
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.
Each line of the input contains a postisive integer n (n <= 1500).Input is terminated by a line with n=0.
For each line, output the n’th ugly number .:Don’t deal with the line with n=0.
1290
1210题意大概是 1个数列其中的数的质因数只能包含2,3,5问第n个这样的数是多少。
优先队列priority_queue 和set用于判断新生成的数是否重复,比如2*3=6 3*2=6
#include <cstdio>#include <cstring>#include <iostream>#include <string>#include <cmath>#include <algorithm>#include <queue>#include <stack>#include <deque>#include <vector>#include <cctype>#include <ctime>#include <map>#include <set>using namespace std;typedef long long ll;const int INF = 0x3f3f3f3f;const int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};#define N 10005#define eps 1e-5#define pai acos(-1)#define MOD 1e9+7ll num[3]={2,3,5};int main(){ #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); ll _begin=clock(); #endif int n; while(scanf("%d",&n)&&n) { priority_queue < ll , vector<ll> ,greater<ll> > s; set<ll> ugly; s.push(1); ugly.insert(1); for(int i=1;;i++) { ll x=s.top(); s.pop(); if(i==n) { printf("%lld\n",x); break; } for(int j=0;j<=2;j++) { ll x2=x*num[j]; if(!ugly.count(x2)) { s.push(x2); ugly.insert(x2); } } } } #ifndef ONLINE_JUDGE ll _end=clock(); printf("%lldms\n",_end-_begin); #endif return 0;}
0 0
- 丑数 简单的优先队列和set
- Uva136-Ugly Numbers-丑数-set集合和优先队列的运用
- Uva136 丑数[优先队列]
- hdu 1058 Humble Numbers 丑数(set,优先队列应用)
- 例题5-7 UVA 136 Ugly Numbers丑数(set+优先队列)
- 丑数(递推||优先队列)
- HDU 1058 Humble Numbers(丑数,优先队列,STL)
- USACO 3.1 Humble Numbers丑数_优先队列
- lintcode 4.丑数 II(优先队列)
- POJ 1338(set, 最小优先队列的简单使用)
- 丑数-直观简单的求法
- 容器set的应用:丑数 UVA - 136
- hdoj-1058(优先队列)---priority_queue====丑数
- 优先队列的简单实现
- 优先队列的简单使用
- 优先队列的简单用法
- 丑数 和 超级丑数
- 进程调度的优先数法和简单轮转法
- 站长的必备工具-站长之家
- 部署CDH以及相关服务启动时的问题与解决方法
- 使用Oracle Instant Client
- Spring-Boot中tomcat端口修改
- 欧里几德及扩展欧里几德算法
- 丑数 简单的优先队列和set
- hdu5632 Rikka with Array 数位dp
- Codeforces Round #402 (Div. 2) (A-D)
- Mybatis入门案例
- 传说中的回调函数练习,也就是指针的深入理解
- 《牛客网剑指offer39题》输入一棵二叉树,判断该二叉树是否是平衡二叉树
- ioctl()函数获取本机ip,mask,mac
- android HTTPURLConnection解决不能访问HTTPs请求
- 【没有assembly目录】spark2.0.0启动时无法访问spark-assembly-*.jar的解决办法