poj - 1338 - Ugly Numbers(优先队列)

题意:问第n(n <= 1500)小的丑数(质因数只有2或3或5)是几。

题目链接:http://poj.org/problem?id=1338

——>>1, 2, 3, 4, 5, 6, 8, ...

假设小根堆存以上丑数,那么每次取出最小的数,这个最小的数nMin,它可以生成三个数:nMin * 2, nMin * 3, nMin * 5,将这三个数放入小根堆继续,一直复筛出1500个丑数为止。

小根堆可用优先队列来替代。

#include <cstdio>
#include <queue>
#include <vector>

using std::priority_queue;
using std::vector;
using std::greater;

const int MAXN = 1500;

long long nRet[MAXN];

void GetUglyNumbers()
{
    int nCnt = 0;
    priority_queue<long long, vector<long long>, greater<long long> > pq;
    pq.push(1);

    while (nCnt < MAXN)
    {
        long long nMin = pq.top();
        pq.pop();
        if (nRet[nCnt] != nMin)
        {
            nRet[++nCnt] = nMin;
            pq.push(nMin * 2);
            pq.push(nMin * 3);
            pq.push(nMin * 5);
        }
    }
}

int main()
{
    int n;

    GetUglyNumbers();
    while (scanf("%d", &n) == 1 && n)
    {
        printf("%I64d\n", nRet[n]);
    }

    return 0;
}
时间: 2024-10-01 02:46:28

poj - 1338 - Ugly Numbers(优先队列)的相关文章

poj 1338 Ugly Numbers(丑数模拟)

转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063? viewmode=contents 题目链接:id=1338">http://poj.org/problem?id=1338 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

poj 1338 Ugly Numbers

Ugly Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21315   Accepted: 9520 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 num

POJ 1338 Ugly Numbers dp

 Ugly Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21867   Accepted: 9767 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

51nod 1010 只包含因子2 3 5的数 &amp;&amp; poj - 1338 Ugly Numbers(打表)

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1010 http://poj.org/problem?id=1338 首先poj这题要找出第n个丑数,想到要打表,因为每一个丑数都是由前一个丑数*2或者*3或者*5得到,每次取3种结果种较小的那一个放入数组中. 打表的时候要注意保持数组有序. 1 #include <iostream> 2 #include <cstdio> 3 #include <c

UVa 136 Ugly Numbers(优先队列)

题意  质因数只可能有2,3,5的数称为丑数  输出第1500个丑数 STL优队列应用  1是丑数 丑数的2,3,5倍都是丑数  用优先队列模拟就行了 #include <cstdio> #include <cstring> #include <set> #include <queue> using namespace std; typedef long long ll; //priority_queue<ll, vector<ll>, g

【优先队列基础练习】POJ1338 Uva136 - Ugly Numbers题解

昨天夜里学了栈.队列和优先队列,栈还有的地方不大懂,队列基本可以,优先队列很高兴.本想今天上午继续弄这个,但是跟着李晨他们一块听了清华交院罗雨屏老师讲的计算几何= =虽然以我的水平听计算几何有点早,但至少是听懂了很多,感觉很高兴:十点多就没有再听,毕竟之后越来越深亚历山大.于是就来写一下丑数的题解. 题目:除2,3,5外不能被其他素数整除的数为丑数,求第1500个丑数(Uva版),求第n(n<=1500)个丑数(POJ版) 思路:第一个是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, 10, 12, 15, ... shows the first 11 ugly numbers. By convention, 1 is included. Write a program to find and print the 1500'th ugly number. Input Ther

poj1338 Ugly Numbers(丑数模拟)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1338 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 number

UVA 136 &amp; POJ1338 Ugly Numbers

题目大意: 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. 把只含有2.3.5因数的数称为丑数,默认第一个丑数是1. POJ是有多次询问,输出第n个丑数 UVA是询问第1500个丑数是多少. 思路: