【蓝桥杯题目】第几个幸运数
到x星球旅行的游客都被发给一个整数,作为游客编号。
x星的国王有个怪癖,他只喜欢数字3,5和7。国王规定,游客的编号如果只含有因子:3,5,7,就可以获得一份奖品。
我们来看前10个幸运数字是:3 5 7 9 15 21 25 27 35 45。 因而第11个幸运数字是:49
小明领到了一个幸运数字 59084709587505,他去领奖的时候,人家要求他准确地说出这是第几个幸运数字,否则领不到奖品。
请你帮小明计算一下,59084709587505是第几个幸运数字。需要提交的是一个整数,请不要填写任何多余内容。
解题思路:dfs或bfs 搜索所有不大于这个数的数。
答案: 1905
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#include <vector>
#include <memory.h>
#include <stack>
typedef long long LL;
using namespace std;
const long long a = 59084709587505;
int Count = 0;
map<LL,bool> mp;
void dfs(LL key)
{
if(key > a)
return ;
Count++;
mp[key]=1;
for(int i = 7; i >= 3; i-=2)
{
if(mp.find(key*i)==mp.end())
dfs(key*i);
}
}
int main()
{
dfs(1);
cout << Count-1 << endl;//除去1的情况
return 0;
}