蓝桥杯-三羊献瑞

三羊献瑞

观察下面的加法算式:

     祥 瑞 生 辉
+   三 羊 献 瑞
-
 三 羊 生 瑞 气

(如果有对齐问题,可以参看【图1.jpg】)

其中,相同的汉字代表相同的数字,不同的汉字代表不同的数字。

请你填写“三羊献瑞”所代表的4位数字(答案唯一),不要填写任何多余内容。

两种方法:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<queue>
 7 using namespace std;
 8 const int INF=0x3f3f3f3f;
 9 #define mem(x,y) memset(x,y,sizeof(x))
10 #define SI(x) scanf("%d",&x)
11 #define PI(x) printf("%d",x)
12 typedef long long LL;
13 int main(){
14     int a,b,c,d,e,f,g,h;
15     for(a=1;a<=9;a++)
16     for(b=0;b<=9;b++)
17     for(c=0;c<=9;c++)
18     for(d=0;d<=9;d++)
19     for(e=1;e<=9;e++)
20     for(f=0;f<=9;f++)
21     for(g=0;g<=9;g++)
22     for(h=0;h<=9;h++){
23         if(a==b)continue;
24         if(a==d)continue;
25         if(a==c)continue;
26         if(a==f)continue;
27         if(a==e)continue;
28         if(a==g)continue;
29         if(a==h)continue;
30         if(b==c)continue;
31         if(b==d)continue;
32         if(b==e)continue;
33         if(b==f)continue;
34         if(b==g)continue;
35         if(b==h)continue;
36         if(c==d)continue;
37         if(c==e)continue;
38         if(c==f)continue;
39         if(c==g)continue;
40         if(c==h)continue;
41         if(d==e)continue;
42         if(d==f)continue;
43         if(d==g)continue;
44         if(d==h)continue;
45         if(e==f)continue;
46         if(e==g)continue;
47         if(e==h)continue;
48         if(f==g)continue;
49         if(f==h)continue;
50         if(g==h)continue;
51         int x=a*1000+b*100+c*10+d;
52         int y=e*1000+f*100+g*10+b;
53         int z=e*10000+f*1000+c*100+b*10+h; 
54         if(x+y==z){
55             printf("%d%d%d%d\n",e,f,g,b);
56         }
57     }
58     return 0;
59 } 
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
typedef long long LL;
int a[10],vis[10];
void dfs(int cur){
    if(cur==8){
        int x=a[0]*1000+a[1]*100+a[2]*10+a[3];
        int y=a[4]*1000+a[5]*100+a[6]*10+a[1];
        int z=a[4]*10000+a[5]*1000+a[2]*100+a[1]*10+a[7]; 
        if(x+y==z){
            printf("%d\n",y);
        }
        return;
    }
    for(int i=0;i<=9;i++){
        if(vis[i])continue;
        if(cur==0&&i==0)continue;
        if(cur==4&&i==0)continue;
        vis[i]=1;
        a[cur]=i;
        dfs(cur+1);
        vis[i]=0;
    }
}
int main(){
    dfs(0);
    return 0;
}

 

 

posted @ 2016-03-01 15:01  handsomecui  阅读(2878)  评论(1编辑  收藏  举报