Problem 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, 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

There is no input to this program.

Output

Output should consist of a single line as shown below, with '<number>' replaced by the number computed.

SampleOutput

The 1500'th ugly number is <number>.

题目大意:

丑数是指不能被2,3,5以外的其他素数整除的数。把丑数从小到大排列起来

结果如下:

1,2,3,4,5,6,8,9,10,12,15,…

求第1500个丑数

思路:

一般暴力求解那肯定会T,所以我们可以借助优先队列将其进行优化,注意的是需要用long long 型,这大家应该也都知道多嘴了ヾ(=゚・゚=)ノ喵♪。

具体看代码:

#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
#define ll long long
const int maxx=;
int main()
{
ll a,b,c;
//优先队列
priority_queue<ll,vector<ll>,greater<ll> >q;
q.push();//初始化为 1;
int num=;//计数;
ll ans;
while(num<maxx)
{
ans=q.top();
q.pop();
num++;
if(num==)
{
printf("The 1500'th ugly number is %d.\n", ans);
break;
}
//如果ans 为ugly ,则2*ans,3*ans,5*ans都是丑数;
a=ans*;
b=ans*;
c=ans*;
// a 如果与 b或c是同一数的因数关系,那么该数一定在b或c中出现过
// 因为b或c比a大
if((a%)&&(a%))
{
q.push(a);
}
if((b%))
{
q.push(b);
}
q.push(c);
}
return ;
}

还有一种操作就是下面这种o(゚Д゚)っ啥!,前提是已经知道结果:

#include<iostream>
using namespace std;
int main()
{
cout<<"The 1500'th ugly number is 859963392.\n";
}

Ugly Numbers UVA - 136(优先队列+vector)的更多相关文章

  1. 丑数(Ugly Numbers, UVa 136)

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

  2. Ugly Numbers UVA - 136

    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 (优先队列)

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

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

  5. 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, ...

  6. 【UVA - 136】Ugly Numbers(set)

    Ugly Numbers Descriptions: Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequ ...

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

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

  8. 136 - Ugly Numbers

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

  9. Ugly Numbers(STL应用)

    题目链接:http://poj.org/problem?id=1338 Ugly Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

随机推荐

  1. topcoder13444

    CountTables TopCoder - 13444 sol:题意和题解都丢在上面了,自己XJByy了一下 先保证行不同,然后对列容斥,dp[i]表示i列的答案 行不同时i列的答案显然是C(c^i ...

  2. CodeForces 631D Messenger —— (kmp的应用)

    这题是一个kmp的应用,思路是有,但是代码实现能力太弱,细节考虑不全,敲了很长时间才AC.. 题意:字符串用如下的方法表示,例如aaabbbbcc表示为3-a,4-b,2-c.那么问t串在s串中出现了 ...

  3. SSM整合(自己收藏)

    https://github.com/crossoverJie/SSM/blob/master/README-ZH.md

  4. Linux设备驱动程序 之 内核符号表

    insmod使用公共内核符号表来解析模块中未定义的符号.功能内核符号表中包含了所有全局内核项(函数和变量)的地址,这是实现模块化驱动程序所必须的.当模块装载到内核后,它所导出的任何符号都会变成内核符号 ...

  5. kafka default partitioner java版本和scala版本的不同

    scala import kafka.utils._ class DefaultPartitioner(props: VerifiableProperties = null) extends Part ...

  6. iView分页组件之分页使用

    基于表格简单示例 <template> <div style="margin:0 auto;"> <Table :loading="load ...

  7. Web实现前后端分离,前后端解耦

    一.前言 ”前后端分离“已经成为互联网项目开发的业界标杆,通过Tomcat+Ngnix(也可以中间有个Node.js),有效地进行解耦.并且前后端分离会为以后的大型分布式架构.弹性计算架构.微服务架构 ...

  8. swoole详解

    1.swoole结构图 2.swoole流程图 3.详细流程图 3.1.Master:处理核心事件驱动(主进程)3.2.Reactor: 处理TCP连接,收发数据的线程.Swoole的主线程在Acce ...

  9. openstack部署cinder

    controller 一.创建cinder数据库并设置权限 mysql -u root -p    CREATE DATABASE cinder;    GRANT ALL PRIVILEGES ON ...

  10. Redis在Linux上面安装

    1 下载Redis:https://redis.io/download 下载完成之后:redis-4.0.11.tar.gz 2 将下载的压缩包传到linux对应文件夹下面(笔者上传到/opt/下面) ...