Ugly Numbers UVA - 136(优先队列+vector)
2024-10-17 02:17:35
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";
}
最新文章
- 【转】基于.NET平台常用的框架整理
- selenium 下载百度音乐并验证
- CSS的::selection使用方法
- PHP中spl_autoload_register()函数的用法
- Shell脚本中执行mysql的几种方式(转)
- Linux GDB常用命令一栏
- Linux进程间通信-匿名管道
- Codevs 1198 国王游戏 2012年NOIP全国联赛提高组
- (极简)linux安装QT4.7.3
- Windows下安装Python3和Django
- Office 2016 永久激活
- embedding与word2vec
- 结对编程——paperOne基于java的四则运算 功能改进
- 做好平衡有多难?谈MMO的职业设计
- SQL Server 索引结构及其使用(二)
- urllib.parse.urldefrag(url)的解释
- python使用外部PY文件的变量
- 【Linux】Linux各文件夹说明
- os模块3
- 再见NullPointerException。在Kotlin里null的处理(KAD 19)