枚举------暴力与优化

枚举

 一、解方程

例:

1957: 2018蓝桥杯培训-枚举专题-day 1 解方程作业题1

题目描述:


    由4个不同的数字,组成的一个乘法算式,它们的乘积仍然由这4个数字组成。

    比如: 

210 x 6 = 1260 
8 x 473 = 3784
27 x 81 = 2187 

    都符合要求。

    如果满足乘法交换律的算式算作同一种情况,那么,包含上边已列出的3种情况,一共有多少种满足要求的算式。

输入:

输出:

#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
typedef pair<int ,int > pp;     //突然想到用set里存pair然后去重!
//弃疗,这个简单枚举的题,就是做不出来!!!,据说  DFS可以,下次体会一下;(划掉,爷做出来了)╭(╯^╰)╮
int main()
{
    int a,b,ans,cnt=0;
    int s[8],t[8];
    set<pp> num;
    //int num[102][3]={0};    //去重,一直栽在这上面;
    for(int i=1;i<10;i++)
    {
        for(int j=0;j<10;j++)
        {
            for(int k=0;k<10;k++)
            {
                for(int p=0;p<10;p++)
                {
                    if(i!=j&&i!=k&&i!=p&&j!=k&&j!=p&&k!=p)//四个互不相同的数字;
                    {
                        //cout << i << " " << j << " " << k << " " << p << " " << endl;
                        int thy=2;
                        s[0]=i,s[1]=j,s[2]=k,s[3]=p;
                        sort(s,s+4);
                        while(thy--)
                        {
                            if(thy==1)
                            {
                                a=i;
                                b=j*100+k*10+p;
                                ans=a*b;
                            }else if(thy==0)
                            {
                                a=i*10+j;
                                b=k*10+p;
                                ans=a*b;
                            }
                            if(ans<1000)
                                continue;
                            //cout << a << " " << b <<  " " << a*b << endl;
                            int w=0;
                            while(w<4)
                            {
                                t[w++]=ans%10;
                                ans/=10;
                            }
                            sort(t,t+4);
                            if(s[0]==t[0]&&s[1]==t[1]&&s[2]==t[2]&&s[3]==t[3])
                            {
                                if(a>b)swap(a,b);
                                pp tmp;
                                tmp.first=a,tmp.second=b;
                                num.insert(tmp);
                               /* if(num[a][0]!=b)
                                {
                                    if(num[a][0]==0)
                                        num[a][0]=b;
                                    else
                                        num[a][1]=b;
                                    cnt++;
                                    cout << a << " " << b <<  " " << a*b << endl;
                                }*/
                                //cout << i << " " << j << " " << k << " " << p << " " << endl;
                            }
                        }
                    }
                }
            }
        }
    }
    cnt=num.size();
    cout << cnt << endl;
    return 0;
}

 

二、字符串处理

 三、用排序对枚举优化

 

 

 

posted @ 2018-09-18 14:24  Akmpfen  阅读(560)  评论(0编辑  收藏  举报