第几个幸运数(蓝桥杯)(优先队列做法)

第几个幸运数

文章目录


前言

优先队列做法

一、题目

到x星球旅行的游客都被发给一个整数,作为游客编号。

x星的国王有个怪癖,他只喜欢数字3,5和7。

国王规定,游客的编号如果只含有因子:3,5,7,就可以获得一份奖品。

我们来看前10个幸运数字是:

3 5 7 9 15 21 25 27 35 45  

因而第11个幸运数字是:49

小明领到了一个幸运数字 59084709587505,他去领奖的时候,人家要求他准确地说出这是第几个幸运数字,否则领不到奖品。

请你帮小明计算一下,59084709587505是第几个幸运数字。

二、代码

#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

typedef long long LL;
typedef pair<LL, int> PII;

const int N = 1e5 + 10;

priority_queue<PII, vector<PII>, greater<PII> > heap;

int main()
{
    heap.push({ 3, 3 });
    heap.push({ 5, 5 });
    heap.push({ 7, 7 });
    LL res = 59084709587505LL;
    int cnt = 1;
    for (int i = 1; ; i++) {
        auto x = heap.top();
        if (x.first == res) {
            cout << cnt << endl;
            break;
        }
        if (x.second == 3) {
            heap.push({ x.first * 3, 3 });
            heap.push({ x.first * 5, 5 });
            heap.push({ x.first * 7, 7 });
            heap.pop();
            cnt++;
        }
        else if (x.second == 5) {
            heap.push({ x.first * 5, 5 });
            heap.push({ x.first * 7, 7 });
            heap.pop();
            cnt++;
        }
        else {
            heap.push({ x.first * 7, 7 });
            heap.pop();
            cnt++;
        }
    }

    system("pause");
    return 0;
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值