4. 丑数 II

设计一个算法,找出只含素因子2,3,5 的第 n 小的数。

符合条件的数如:1, 2, 3, 4, 5, 6, 8, 9, 10, 12…

样例
如果n = 9, 返回 10

挑战
要求时间复杂度为O(nlogn)或者O(n)

注意事项
我们可以认为1也是一个丑数

用优先队列实现,C++ STL的优先队列即一个堆

priority_queue的用法

  1. 头文件是#include

  2. 关于priority_queue中元素的比较

模板申明带3个参数:priority_queue<Type, Container, Functional>,其中Type 为数据类型,Container为保存数据的容器,Functional 为元素比较方式。

Container必须是用数组实现的容器,比如vector,deque等等,但不能用 list。STL里面默认用的是vector。

  1. 比较方式默认用operator<,所以如果把后面2个参数缺省的话,优先队列就是大顶堆(降序),队头元素最大。

若数据类型为pair,则先按pair的first排序,若first相同再按second排序

思路就是通过不断与2,3,5因子相乘并排序,得到丑数序列,最后输出第n个丑数即可

代码如下

class Solution {
public:
    /**
     * @param n: An integer
     * @return: the nth prime number as description.
     */
    typedef pair<unsigned long, int> node_type; 
    int nthUglyNumber(int n) {
        // write your code here
        unsigned long result[2000];
        priority_queue<node_type ,vector<node_type>, greater<node_type> > Q;
        Q.push(node_type(1,2));
        for(int i=0; i<2000; i++)
        {
            node_type node= Q.top();
            Q.pop();
            switch(node.second)
            {
                case 2:
                Q.push(make_pair(node.first*2,2));
                case 3:
                Q.push(make_pair(node.first*3,3));
                case 5:
                Q.push(make_pair(node.first*5,5));
            }
            result[i]=node.first;
        }
        return result[n-1];
    }
};

这里生成了2000个丑数,应该够用了,当然也可以只生成n个丑数,这样应该会更快一点

附:非丑数的求法

题目如下

18005 It is not ugly number
时间限制:2000MS 内存限制:65535K
提交次数:0 通过次数:0

题型: 编程题 语言: G++;GCC
Description
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence1, 2, 3, 4, 5, 6, 8, 9, 10, 12, …shows the first 10 ugly numbers. By convention, 1 is included. Then, here are the first 10 Not ugly numbers:7, 11, 13, 14, 17, 19, 21, 22, 23, 26. Given the integer n, write a program to find and print the n’th Not ugly number.

输入格式
First line is T(T<=10000), the number of cases.
The T lines following. Each line of the input contains a positive integer n (n <= 100000000).

输出格式
For each case, output the n’th Not ugly number .

输入样例
3
1
2
9

输出样例
7
11
23

找到非丑数的思路如下:

因为自然数中只包含丑数和非丑数,所以只需要知道

丑数的个数+非丑数的个数=当前数的值

如丑数8=7个丑数(1,2,3,4,5,6,8)的个数加一个非丑数(7)
那么,非丑数7就等于7+1-1=7(7个丑数加一个非丑数为丑数8,再减去丑数8的个数1,即为第一个非丑数)

因此,每个丑数的值val=n+j

这里n为非丑数的个数,j为丑数的个数

找到丑数之后,第n个非丑数的值即为这个丑数-1,即n+j-1,意思是n个非丑数的个数加j-1个丑数的个数就是第n个非丑数的值

代码如下

#include <iostream>
#include <cmath>
#include <queue>
using namespace std;
typedef pair<unsigned long, int> node_type;
int main()
{
    unsigned long result[1500];
    priority_queue<node_type ,vector<node_type>, greater<node_type> > Q;
    Q.push(node_type(1,2));
    for(int i=0; i<1500; i++)
    {
        node_type node= Q.top();
        Q.pop();
        switch(node.second)
        {
            case 2:
            Q.push(make_pair(node.first*2,2));
            case 3:
            Q.push(make_pair(node.first*3,3));
            case 5:
            Q.push(make_pair(node.first*5,5));
        }
        result[i]=node.first;
    }
    int i,j,T,a;
    /*for(i=0;i<1500;i++)
    {
        cout<<result[i]<<endl;
    }*/
    cin>>T;
    for(i=0;i<T;i++)
    {
        int n;
        cin>>n;
        for(j=0;j<1500;j++)
        {
            if(result[j]>n+j)
            {
                cout<<n+j<<endl;
                break;
            }
        }
    }
    return 0;
}

因为数组从0开始,所以if里面的比较为大于,结果也不需要减一

关于优先队列的部分内容转载自
https://www.cnblogs.com/Deribs4/p/5657746.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值