简单枚举算法(simple enumeration)

枚举算法(穷举),是指在一个有穷的、可能的解集合中,枚举出集合中的每一个元素,判断该元素是否符合题目的检验条件,得出解集。
枚举法是一种搜索算法,即对问题的所有可能解状态进行一次遍历。

三位邮箱密码可能性

邮箱密码是 3 位数,最高位是 6,并且能被 18 整除。这个密码可能有多少种?

一、枚举法求质数

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main(){
    long long m;
    cin>>m;
    bool IsPrime=1;
    for (int i = 2; i*i <m; i++){
        if(m%i==0){
        IsPrime=0;
        break;
        }
    }
    if(IsPrime)
        cout<<"yes";
    else
        cout<<"no";
return 0;
}

二、求满足下面等式的a,b,c的取值(a,b,c取值范围:1<a,b,c<100)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <iostream>
using namespace std;

int main(){
    int a,b,c;
    for (int a=1; a < 100; a++){
        for ( b = a;  b<100; b++){
            for (c=b; c < 100; c++)            {
                if((1+a)*(1+b)*(1+c)==2*a*b*c)
                    cout<<a<<' '<<b<<' '<<c<<endl;
            }
        }
    }
}



P1008 [NOIP1998 普及组] 三连击
OJ:P1406

孪生素数; 洛谷:B2132
OJ:T1096, P1179