第九届蓝桥杯——第几个幸运数

【问题描述】

到x星球旅行的游客都被发给一个整数,作为游客编号。
x星的国王有个怪癖,他只喜欢数字3,5和7。
国王规定,游客的编号如果只含有因子:3,5,7,就可以获得一份奖品。
我们来看前10个幸运数字是:3 5 7 9 15 21 25 27 35 45
因而第11个幸运数字是:49
小明领到了一个幸运数字 59084709587505,他去领奖的时候,人家要求他准确地说出这是第几个幸运数字,否则领不到奖品。
请你帮小明计算一下,59084709587505是第几个幸运数字。

【答案提交】
需要提交的是一个整数,请不要填写任何多余内容。


暴力:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

typedef long long LL;

LL N = 59084709587505;

int main() 
{
	int ans=0;
	for(int i = 0; pow(3, i) <= N; i ++)
	for(int j = 0; pow(5, j) <= N; j ++)
	for(int k = 0; pow(7, k) <= N; k ++)
		if(pow(3, i) * pow(5, j) * pow(7, k) <= N) ans ++;
	
	// 减去 i,j,k 都为 0 的情况			
	cout << ans - 1 << endl;
	return 0;
}

看不懂的做法一
set:

#include <cstdio>
#include <cstring> 
#include <iostream>
#include <algorithm>
#include <set>

using namespace std;
 
typedef long long LL;

set<LL>s;
LL a[3] = {3, 5, 7};
LL tt, t = 1;

int main()
{
    while(true)
    {
        for(int i = 0; i < 3; i ++)
        {
            tt = t * a[i];
            if(tt <= 59084709587505)
                s.insert(tt);
        }
        t = *s.upper_bound(t);
        if(t == 59084709587505)
            break;
    }
    
    cout << s.size() << endl;
    return 0;
}

看不懂的写法二
priority_queue:

#include <iostream>
#include <vector>
#include <queue>
#include <set>

using namespace std;
 
priority_queue<long long,vector<long long>,greater<long long> > q;
set<long long> s;
 
int main()
{
	int ans = -1;
	q.push(1);
	s.insert(1);
	while(1)
	{
		long long n = q.top();
		q.pop();
		ans ++;
		if(n == 59084709587505)
			break;
		for(int i = 3; i <= 7; i += 2)
		{
			long long t = n * i;
			if(!s.count(t))
			{
				q.push(t);
				s.insert(t);
			}
		}
	}
	cout << ans << endl;
	return 0;
}

答案:1905

如果感觉这篇文章对你有帮助的话,不妨点一个赞,十分感谢(✪ω✪)。
printf(“点个赞吧!”);
cout <<“点个赞吧!”;
System.out.println(“点个赞吧!”);
↓↓↓

发布了63 篇原创文章 · 获赞 5 · 访问量 828

猜你喜欢

转载自blog.csdn.net/weixin_46239370/article/details/105483056