Uva 136-丑数 ugly number

题目描述:

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 and Output

There is no input to this program. Output should consist of a single line as shown below, with <number> replaced by the number computed.

Sample output

The 1500‘th ugly number is <number>.

分析:

本题难点不多,直接使用队列完成。

优先队列priority_queque的详细用法,请戳(http://www.cnblogs.com/void/archive/2012/02/01/2335224.html

代码:

 1 #include<iostream>
 2 #include<set>
 3 #include<queue>
 4 #include<vector>
 5 using namespace std;
 6 typedef long long ll;
 7 const int basic[3]={2,3,5};
 8 int main()
 9 {
10     priority_queue<ll,vector<ll>,greater<ll> >pq;
11     set<ll>q;
12     q.insert(1);
13     pq.push(1);
14     for(int i=1;;i++)
15     {
16         ll x=pq.top();
17         pq.pop();
18         if(i==1500)
19         {
20             cout<<"The 1500‘th ugly number is "<<x<<".\n";
21             break;
22         }
23         for(int j=0;j<3;j++)
24         {
25             ll x2=basic[j]*x;
26             if(!q.count(x2))
27             {
28                 q.insert(x2);
29                 pq.push(x2);
30             }
31         }
32     }
33     return 0;
34  } 
时间: 2024-08-25 11:57:26

Uva 136-丑数 ugly number的相关文章

UVa 136 丑数

背景;开始没有充分理解题意:不能被2,3,5以外的其它素数整除.在整除的数学中,素数相当于基,任何除了一以外的数,都是由素数基相乘而得. 思路:有第一个丑数1,开始,每一个丑数*2,*3,*5生成下一个丑数.这样依次生成. 学习: 1.算术基本定理:每一个大于2的数总是由素数因子相乘而得,且个素数因子的个数是确定的. 我的代码: #include<iostream> #include<set> using namespace std; typedef long long ll; /

[Swift]LeetCode263. 丑数 | Ugly Number

Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. Example 1: Input: 6 Output: true Explanation: 6 = 2 × 3 Example 2: Input: 8 Output: true Explanation: 8 = 2

两种方法求丑数

我们把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 方法1 : 暴力破解,逐个判断 代码: <pre name="code" class="cpp">#include <iostream> #include <vector> using namespace std; //判断是否是丑数 bool isUgly(int index){ while(index % 2

【面试题034】丑数

[面试题034]丑数 题目: 我们把只包含因子2.3和5的数称为丑数(Ugly Number). 求按从小到大的顺序的第1500个丑数. 例如6.8都是丑数,但14不是,因为他包含因子7.习惯上我们把1当做第一个丑数. 思路一: 逐个的判断,效率不高. 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647   #include <iostream>using namespace

算法分析---寻找丑数

什么是丑数: 一个数的因子仅仅包括2,3,5的数称为丑数.数字1特别对待也看作是丑数,所以从1開始的10个丑数分别为1,2.3.4,5,6,8,9.10.12. 因子的概念: 整数m除以n,得到无余数的商,则称n是m的一个因子.如8的因子有1.2.4.8.而丑数要求的因子仅仅包括2.3.5.所以丑数中的因子应理解为质因子. 即因子为质数.质数又称素数,指一个大于1的自然数.除了1和它自身外,不能被其它自然数整除的数. 与质数相相应的数称为合数. 如今要求写一个程序,输出从1開始的第N个丑数. 如

263. Ugly Number(C++)

263. Ugly Number Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.

37.寻找丑数

http://zhedahht.blog.163.com/blog/static/2541117420094245366965/ http://www.cppblog.com/zenliang/articles/131094.html http://www.cnblogs.com/coser/archive/2011/03/07/1976525.html 题目:我们把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7.习惯上我们把1当做是

《剑指offer》第四十九题(丑数)

// 面试题49:丑数 // 题目:我们把只包含因子2.3和5的数称作丑数(Ugly Number).求按从小到 // 大的顺序的第1500个丑数.例如6.8都是丑数,但14不是,因为它包含因子7. // 习惯上我们把1当做第一个丑数. #include <iostream> // ====================算法1的代码==================== //不用额外的内存,直接计算 bool IsUgly(int number)//判断是不是丑数 { while (num

POJ1338——丑数

问题 丑数是仅有素因子2.3和5的整数.序列1,2,3,4,5,6,8,9,10,12是前十个丑数.1被包含在丑数中. 输入 给出一个正整数n(n<=1500) 输入0表示结束 输出 对于输入的每一行,输出第n个丑数 分析 预先算出第1-1500个丑数存入数组中,从小到大排列,然后取出第n个输出即可. 由于丑数仅有2.3.5这三个因子,所以集合中的元素都是通过乘以2.3.5这三个因子扩展获得的. 比如: ugly[1] =1 ugly[2]=2 ,用第1个丑数 ugly[1]分别以从小到大的顺序