The first few lucky number (Ninth Provincial Blue Bridge Cup competition C ++ A group)

The first few lucky number

X travel to the planet visitors are sent to an integer, as a tourist numbers. X king star has a knack, he only likes numbers 3, 5 and 7. King provisions, if the number of visitors contains only factor: 3,5,7, you can get a prize. Let's look at the first 10 lucky numbers are: 3,579,152,125,273,545 so the first 11 lucky numbers are: 49 Xiao Ming received a lucky number 59084709587505, he went to accept the award, when people asked him precisely this is the first of several lucky number, otherwise not receive prizes. Xiao Ming to help you calculate, 59,084,709,587,505 are a few lucky number. Is required to submit an integer, please do not fill out any extra content.

This question I do not very familiar with stl, take a lot of detours is true food, there is a usage summary of bloggers set the container.

(https://www.cnblogs.com/zyxStar/p/4542835.html)

Thinking HOL removed, when the head <= lucky number, the head successively with 3 * 5 * 7 * Add the tail, and finally to scan again set. elements are set in order not to repeat the (so delicious).

The Code ,,,,,,,,,,,,,,,,,

#include<iostream>
#include<set>
#define LL long long
using namespace std;
LL maxs=59084709587505;
set<LL>q;
int main()
{
    q.insert(3);
    q.insert(5);
    q.insert(7);
    set<LL>::iterator it;
    it=q.begin();
    LL mid;
    while(*it<=maxs)
    {
        mid=*it;
        q.insert(mid*3);
        q.insert(mid*5);
        q.insert(mid*7);
        it++;
    }
    int num=0;
    for(it=q.begin();it!=q.end();it++)
    {
        if(*it<=maxs)num++;
    }
    cout<<num;
    return 0;
}
Published 10 original articles · won praise 0 · Views 181

Guess you like

Origin blog.csdn.net/weixin_44460602/article/details/104757790