题目链接:

https://cn.vjudge.net/problem/UVA-136

 /*问题
输出第1500个丑数,丑数的定义是不能被2,3,5以外的其他素数整除的数 解题思路
直接硬暴力先试一下发现枚举的时候不知道它能被那个素数整除,况且打很大的素数表依次遍历也不一定能够找到1500个丑数。
那么根据定义,如果x是一个丑数,那么2*x、3*x、5*x也一定是丑数。那么构建一个优先队列,让数值小的优先级最高,加入第一个丑数1,
依次加入三个丑数(数值小的在前面),直到第1500个丑数时结束,另外可能生成的丑数会重复,因此需要一个set集合来记录哪些数已经
出现过了。
*/
#include<cstdio>
#include<iostream>
#include<cmath>
#include<vector>
#include<set>
#include<queue>
using namespace std;
const int m[]={,,}; typedef long long LL; int main()
{
set<LL> s;
priority_queue<LL,vector<LL>,greater<LL> > pq; s.insert();
pq.push();
for(int i=;;i++){
LL x=pq.top();
pq.pop(); if(i == ){
cout<<"The 1500'th ugly number is "<<x<<'.'<<endl;
break;
}
for(int j=;j<;j++){
LL x1=x*m[j];
if(!s.count(x1)){
s.insert(x1);
pq.push(x1);
}
}
}
return ;
}

136 Ugly Numbers(priority_queue+逆向求解要求数)的更多相关文章

  1. UVA.136 Ugly Numbers (优先队列)

    UVA.136 Ugly Numbers (优先队列) 题意分析 如果一个数字是2,3,5的倍数,那么他就叫做丑数,规定1也是丑数,现在求解第1500个丑数是多少. 既然某数字2,3,5倍均是丑数,且 ...

  2. 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 ...

  3. UVA - 136 Ugly Numbers (有关set使用的一道题)

    Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence1, 2, 3, 4, 5, 6, 8, 9, ...

  4. 136 - Ugly Numbers

     Ugly Numbers  Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3 ...

  5. UVa 136 Ugly Numbers【优先队列】

    题意:给出丑数的定义,不能被除2,3,5以外的素数整除的的数称为丑数. 和杭电的那一题丑数一样--这里学的紫书上的用优先队列来做. 用已知的丑数去生成新的丑数,利用优先队列的能够每次取出当前最小的丑数 ...

  6. UVa 136 - Ugly Numbers

    题目大意:只有素因子2,3,5的数叫做丑数.输出第1500个丑数即可. 这个...好吧,直接输出就是了.自己写一个小程序先计算一下,这就是黑盒测试的好处啊,“我们的目标是解决问题,而不是为了写程序而写 ...

  7. 丑数(Ugly Numbers, UVa 136)

    丑数(Ugly Numbers, UVa 136) 题目描述 我们把只包含因子2.3和5的数称作丑数(Ugly Number).求按从小到大的顺序的第1500个丑数.例如6.8都是丑数,但14不是,因 ...

  8. Ugly Numbers UVA - 136(优先队列+vector)

    Problem Description Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, ...

  9. UVa136 Ugly Numbers(优先队列priority_queue)

    Ugly Numbers 题目 Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, ...

随机推荐

  1. 为什么有时候在mac中启动一些应用时会跳的Windows中

    因为有些格式是尽在windows情况下才支持的,如果mac上安装了虚拟的Windows,则执行此类应用(例如:.bat 文件)时就会启动Windows虚机

  2. hdu 1.3.3 今年暑假不AC

    //简单.... 1 #include<iostream> #include<cstdio> #include<cstdlib> using namespace s ...

  3. 一致性hash(整理版)

    简单解释: 简单解释一致性hash的原理:网上通篇都是用服务器做的举例,我这里也如此,主要是便于理解. 通常:有N个客户端请求服务器,假设有M台web服务器,通常为了均衡访问会进行N%M的取模,然后分 ...

  4. cnblogs第一篇文章

    大家好,以后我就在这里很多交流分享了!谢谢!

  5. 实验8 LCD8*8点阵

    1.控制点阵红绿交替显示,分别从上到下,从左到右循环闪烁三次 接线: P0接J12.P1接J20.P2接J19 /** 1.控制点阵红绿交替显示,分别从上到下,从左到右循环闪烁三次 **/ #incl ...

  6. [leetcode.com]算法题目 - Pascal's Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  7. Android开发教程 - 使用Data Binding(一) 介绍

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

  8. JQuery - on绑定多个事件

    一.jquery为多个选择器绑定同一个事件 $("#start,#end").on("click",function(){ alert("The pa ...

  9. [学习笔记]Link-Cut Tree

    我终于理解了 \(LCT\)!!!想不到小蒟蒻有一天理解了!!! 1.[模板]Link Cut Tree 存个板子 #include <bits/stdc++.h> using names ...

  10. 关于 IPv6

    http://test-ipv6.com/ http://bbs.chinaunix.net/thread-1799798-1-1.html