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. 灰度变换,gama变换,对数,反对数变换

    学习DIP第2天 灰度变换,及按照一定规则对像素点的灰度值进行变换,变换的结果可以增强对比度,或者达到其他的效果(例如二值化,或者伽马变换),由于灰度变换为针对单个像素点的灰度值进行变换,素以算法复杂 ...

  2. 生产者和消费者之间的线程通讯wait()

    生产者与消费者,采用notify()唤醒 package com.dwz.concurrency.chapter9; /** * 生产者和消费者之间的通信问题 * 执行wait()之后锁被释放 */ ...

  3. HDU 2176 取(m堆)石子游戏 —— (Nim博弈)

    如果yes的话要输出所有情况,一开始觉得挺难,想了一下也没什么. 每堆的个数^一下,答案不是0就是先取者必胜,那么对必胜态显然至少存在一种可能性使得当前局势变成必败的.只要任意选取一堆,把这堆的数目变 ...

  4. springboot 底层 JackSon 的使用

    Jackson常用的注解使用和使用场景: 接下来我们在看一段代码,这段代码是常用注解在实体类User中的简单使用:package zone.reborn.springbootstudy.entity; ...

  5. LC 979. Distribute Coins in Binary Tree

    Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there ar ...

  6. easyUI之progressbar进度条

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  7. jdk1.8 HashMap & ConcurrentHashMap

    JDK1.8逐字逐句带你理解ConcurrentHashMap https://blog.csdn.net/u012403290 JDK1.8理解HashMap https://blog.csdn.n ...

  8. Ubuntu16.04格式化U盘

    root@ubuntu:~# fdisk -l root@ubuntu:~# fdisk /dev/sdb 格式化U盘: root@ubuntu:~# fdisk -l sudo mkfs.ntfs ...

  9. 《计算机系统要素》第四章 类汇编语言 Hack

    这章通过学习书中自己设计的Hack语言的使用,弄懂汇编语言的工作原理. 汇编语言最接近底层了,因为每个指令对应一个二进制编码. 当这些指令都变成...0101011100101...的形式后,内存Me ...

  10. 手写web框架之实现Bean容器

    实现Bean容器    使用ClassHelper可以获取所加载的类,但无法通过类来实例化对象,因此我们需要提供一个反射工具类,让它封装java反射相关的API,对外提供更好用的工具方法.将该类命名为 ...