有趣的数 数组筛选法

我们称一个数是质数,而且数位中出现了 5 的数字是有趣的。例如 5,59,457 都是有趣的,而 15,7不是。求 1 到100000 中有趣的数的个数。

主要考虑两个问题:

1.从1-100000中筛选出质数

2.对筛选出来的质数进行判断是否满足有趣条件

#include<bits/stdc++.h>
using namespace std;
int Num = 0;
int Find(int a){
    while(a)
    {
        if(a%10 == 5){
            return Num++;
        }else{
            a/=10;
        }
    }
    return 0;
}


int main(){
    int str[100000] = {};
    int flag;
    str[0] = str[1]=1;
    for(int i = 2;i<100000;i++)
    {
        if(str[i]!=1){
            Find(i);
            flag = 2;
            while(i*flag<100000)
            {
                str[i*flag] = 1;
                flag++;
            }
        }
    }
    
    cout <<Num;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/while_1_/article/details/104450653