UVALive 6163(暴力枚舉)

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   art   

這道題我的做法就是枚舉這四個數的所有排列所有運算所有計算順序。

略有考驗代碼能力,不能漏掉情況,注意模組化的思想,一些功能寫成函數調試的時候結構清晰好分析。

比賽時沒有AC是對next_permutation()函數理解的不透,根本沒有想到是沒有從最小字典序開始枚舉的問題。

就是next_permutation()函數是從當前順序枚舉到字典序最大的,而我開始時do裡面的a數組不一定是字典序最小的,但是next_permutation()函數一定是從當前字典序往最大的枚舉,所以漏掉了字典序很小的那些情況。所以我在do裡面第一步加一個sort排序就AC了。

next_permutation()函數是從當前順序一直枚舉到字典序最大的(按字典序遞增的順序),沒有更大的排列了就返回false,否則改了排列順序以後返回true。

prev_permutation()函數是從當前順序一直枚舉到字典序最小的(按字典序遞減的順序),沒有更小的排列了就返回false,否則改了排列順序以後返回true。

這也是next和prev的意思,所謂“下一個”和“上一個”,就是字典序剛好比它大一點的排列和剛好比它小一點的排列。

平時學知識一知半解,比賽時就遭報應了。。。。。。

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<map>#include<set>#include<vector>#include<algorithm>#include<stack>#include<queue>using namespace std;#define INF 1000000000#define eps 1e-8#define pii pair<int,int>#define LL long long intint n,a[4];char in[4];bool deal();bool cal(int i,int j,int k);int g(int x,int y,int d);int main(){    //freopen("in1.txt","r",stdin);    //freopen("out.txt","w",stdout);    while(scanf("%d",&n)==1&&n)    {        bool ans=true;        for(int i=1; i<=n; i++)        {            scanf("%s",in);            if(ans==false)                continue;            else            {                a[0]=in[0]-‘0‘;                a[1]=in[1]-‘0‘;                a[2]=in[2]-‘0‘;                a[3]=in[3]-‘0‘;                if(deal()==false)//這四個數弄不出10                {                    ans=false;                }            }        }        if(ans==true)            printf("TRUE\n");        else            printf("BUSTED\n");    }    //fclose(stdin);    //fclose(stdout);    return 0;}bool deal(){    sort(a,a+4);//沒有這句就WA了    do    {        for(int i=0; i<4; i++)        {            for(int j=0; j<4; j++)            {                for(int k=0; k<4; k++)                {                    //三個for枚舉所有運算子的組合                    if(cal(i,j,k))                    {                        return true;                    }                }            }        }    }    while(next_permutation(a,a+4));//枚舉四個數的所有排列    return false;}bool cal(int i,int j,int k){    int t1=g(a[0],a[1],i);    int t2=g(a[1],a[2],j);    int t3=g(a[2],a[3],k);    //接下來枚舉所有運算次序    if(g(t1,t3,j)==10)//k,i,j        return 1;    else if(  g( g(a[0],t2,i),a[3],k)==10 )//j,i,k        return 1;    else if(  g( g(t1,a[2],j),a[3],k)==10 )//i,j,k        return 1;    /*else if(  g( g(a[0],a[1],i),t3,j)==10 )//i,k,j        return 1;*/    else if(  g( a[0],g(t2,a[3],k),i)==10 )//j,k,i        return 1;    else if(  g( a[0],g(a[1],t3,j),i)==10 )//k,j,i        return 1;    else        return 0;}int g(int x,int y,int d){    if(d==0)        return x+y;    else if(d==1)        return x-y;    else if(d==2)        return x*y;    else    {        if(y)            return x/y;        else            return 0;//分母為0不能進行除法,那這種情況肯定不行,那我就看成乘以0返回。    }}
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.