Ugly Numbers UVA - 136(优先队列+vector)
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)的更多相关文章
- 丑数(Ugly Numbers, UVa 136)
丑数(Ugly Numbers, UVa 136) 题目描述 我们把只包含因子2.3和5的数称作丑数(Ugly Number).求按从小到大的顺序的第1500个丑数.例如6.8都是丑数,但14不是,因 ...
- 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 ...
- UVA.136 Ugly Numbers (优先队列)
UVA.136 Ugly Numbers (优先队列) 题意分析 如果一个数字是2,3,5的倍数,那么他就叫做丑数,规定1也是丑数,现在求解第1500个丑数是多少. 既然某数字2,3,5倍均是丑数,且 ...
- 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 ...
- 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, ...
- 【UVA - 136】Ugly Numbers(set)
Ugly Numbers Descriptions: Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequ ...
- UVa136 Ugly Numbers(优先队列priority_queue)
Ugly Numbers 题目 Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, ...
- 136 - Ugly Numbers
Ugly Numbers Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3 ...
- Ugly Numbers(STL应用)
题目链接:http://poj.org/problem?id=1338 Ugly Numbers Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
随机推荐
- Appium基础教程
目录 Appium教程 Appium简介 App自动化测试工具对比 Appium实现原理 环境搭建 Andorid介绍 基本架构 常见布局/视图 基本控件 控件常见属性 Adb介绍 Adb常用命令 A ...
- sublime中替换成换行
ctrl + h 打开替换窗口. replace with里输入 ctrl + shift + enter
- cache magic对pms模块的cache访问模式分析结果
其中43.184的命中率只有不到70%,是要分析的对象:3.189是命中率98%左右,是做参考的对象. 基本统计:可以知道43.184的update和delete操作占总操作的比例要比3.189打不少 ...
- ubuntu下如何使用apt-get安装arm64的交叉编译工具链?
答: sudo apt-get install gcc-aarch64-linux-gnu -y
- ios 报错记录
1. 运行xcode 报错:unterminated conditional directive #ifdef 缺少对应的#endif 在结尾加上就好了 2.iOS添加非(c,c++)文件引发的&qu ...
- matlab处理矩阵
1.提取大矩阵的一列.一行元素:一列元素: A(:,j)表示提取A矩阵的第j列全部元素一行元素: A(i,:)表示提取A矩阵的第i行元素,于是我们有,A(i, j)表示提取A矩阵的第i行第j列的元 ...
- 异步发送表单数据到JavaBean,并响应JSON文本返回
1) 提交表单后,将JavaBean信息以JSON文本形式返回到浏览器 <form> 编号:<input type="text" name="id&q ...
- 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-1.整合Mybatis访问数据库和阿里巴巴数据源
笔记 1.整合Mybatis访问数据库和阿里巴巴数据源 简介:整合mysql 加入mybatis依赖,和加入alibaba druid数据源 1.加入依赖(可以用 http://start.s ...
- ng build --aot 与 ng build --prod
angluar的编译有以下几种方式: ng build 常规的压缩操作 代码体积最大 ng build --aot angular预编译 代码体积较小 ng build --pr ...
- GWO(灰狼优化)算法MATLAB源码逐行中文注解(转载)
以优化SVM算法的参数c和g为例,对GWO算法MATLAB源码进行了逐行中文注解. tic % 计时器 %% 清空环境变量 close all clear clc format compact %% ...