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";
}

最新文章

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

热门文章

  1. 云栖社区 Tensorflow快餐教程
  2. codeforces163D
  3. 集合家族——LinkedList
  4. JS基础_call和apply
  5. fastadmin编辑内容,有下拉选择关联的内容,自定义的参数去获取相应的下拉内容
  6. DataFactory生产手机号码
  7. 1.2 位于Shell脚本第一行的#!
  8. C realloc
  9. easyUI之Messager(消息窗口)
  10. vector subscript out of range