UVA 136 Ugly Number

UVA 136 Ugly Number

题目描述

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.

输入样式

There is no input to this program. Output should consist of a single line as shown below, with replaced by the number computed.

输出样式

The 1500'th ugly number is .


知识补充:C++ stl priority_queue

  1. 优先队列的出队顺序不是简单的先进先出,而是依照优先级顺序进行操作

  2. 定义方式

    priority_queue,greater>
    

代码实现

//摘自刘汝佳
#include
#include
#include

using namespace std;

typedef long long ll;
queue pq;
const conse={2,3,5};

int main(){
    priority_queue,greater >pq;
    set s;
    pq.push(1);
    s.insert(1);
    for(i=1;;i++){
        ll t=pq.top();
        pq.pop();
        if(i==1500){
        printf("%d",t);
        break;}
        for(j=0;j<3;j++){
            ll x=conse[i]*t;
            if(!s.count[x]){
            s.insert(x);
            pq.insert(x);   
            }   
        }
    }
    return 0;
} 

By制杖菜鸡

你可能感兴趣的:(UVA 136 Ugly Number)