等边三角形

有如下一个题目:

 

 

 

 

 

我就直接for循环套,暴力求解。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define test cout<<"***"<<endl
 4 int n;
 5 int an[15];
 6 int main(){
 7     cin>>n;
 8     int cnt = 0;
 9     memset(an,0,sizeof(an));
10     int a,b,c,d,e,f,g,h,i;
11     for(int i1=1;i1<=9;i1++){
12         // test;
13         a = i1;
14         an[i1] = 1;
15         for(int j=i1+1;j<=9;j++){
16             f = j;
17             an[j] = 1;
18             for(int k=j+1;k<=9;k++){
19                 i = k;
20                 an[k] = 1;
21                 for(int p = 1;p<=9;p++){
22                     if(an[p]==0){
23                         b = p;
24                         an[p] = 1;
25                         for(int p1 = b;p1<=9;p1++){
26                             if(an[p1]==0){
27                                 d = p1;
28                                 an[p1] = 1;
29                                 for(int p2=1;p2<=9;p2++){
30                                     if(an[p2]==0){
31                                         c = p2;
32                                         an[p2] = 1;
33                                         for(int p3 = c;p3<=9;p3++){
34                                             if(an[p3]==0){
35                                                 e = p3;
36                                                 an[p3] = 1;
37                                                 for(int p4 =1;p4<=9;p4++){
38                                                     if(an[p4]==0){
39                                                         g = p4;
40                                                         an[p4] = 1;
41                                                         for(int p5 =g;p5<=9;p5++){
42                                                             if(an[p5]==0){
43                                                                 h = p5;
44                                                                 an[p5]= 1;
45                                                                 int x=a+b+d+f;
46                                                                 int y=f+g+h+i;
47                                                                 int z=i+e+c+a;
48                                                                 if(x==y&&y==z&&x==n){
49                                                                     cnt++;
50                                                                 //    cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" "<<f<<" "<<g<<" "<<h<<" "<<i<<endl;
51                                                                 }
52                                                                 an[p5]=0;
53                                                             }
54                                                         }
55                                                         an[p4] = 0;
56                                                     }
57                                                 }
58                                                 an[p3] = 0;
59                                             }
60                                         }
61                                         an[p2] = 0;
62                                     }
63                                 }
64                                 an[p1] = 0;
65                             }
66                         }
67                         an[p] = 0;
68                     }
69                 }
70                 an[k] = 0;
71             }
72             an[j] = 0;
73         }
74         an[i1]=0;
75     }
76     if(cnt)
77         cout<<cnt<<endl;
78     else
79         cout<<"No"<<endl;
80     return 0;
81 }

 

下面是后台测试数据(过了说明代码就行了):

23 2
18 No
19 4
20 6
21 4

posted @ 2018-01-10 15:22  #忘乎所以#  阅读(271)  评论(0编辑  收藏  举报