题解 P1008 【三连击】

2017-12-16 18:27:43


机智的九循环

https://www.aptx.xin/cp1008.html


    #include<cstdio>
    #include<iostream>
    using namespace std;
    bool vis[10]; //布尔数组判断用没用过
    int main(){
        int a,b,c,d,e,f,g,h,i;
        int sum1,sum2,sum3;
        for(a=1;a<=9;++a) { vis[a]=1;   //九循环
            for(b=1;b<=9;++b) { if (vis[b]) continue; vis[b]=1;
                for(c=1;c<=9;++c) { if (vis[c]) continue; vis[c]=1;
                    for(d=1;d<=9;++d) { if (vis[d]) continue; vis[d]=1;
                        for(e=1;e<=9;++e) { if (vis[e]) continue; vis[e]=1;
                            for(f=1;f<=9;++f) { if (vis[f]) continue; vis[f]=1;
                                for(g=1;g<=9;++g) { if (vis[g]) continue; vis[g]=1;
                                    for(h=1;h<=9;++h) { if (vis[h]) continue; vis[h]=1;
                                        for(i=1;i<=9;++i) {if (vis[i]) continue;
                                        sum1=100*a+10*b+c;
                                        sum2=100*d+10*e+f;
                                        sum3=100*g+10*h+i;
                                        if(sum1*2==sum2&&sum1*3==sum3)//1比2比3
                                        cout<<sum1<<" "<<sum2<<" "<<sum3<<endl;    
                                        }
                                    vis[h]=0;}
                                vis[g]=0;}    
                            vis[f]=0;}
                        vis[e]=0;}
                    vis[d]=0;}
                vis[c]=0;}
            vis[b]=0;}
        vis[a]=0;}
}