hiho1304 24点

题目链接:https://hihocoder.com/problemset/problem/1304

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 int num[4],nums[4];
 5 char op[6]={'+','-','*','/','#','$'};
 6 char ops[3];
 7 
 8 int vis[4];
 9 
10 double cal(double x,double y,char oop)
11 {
12     switch(oop)
13     {
14         case '+': return x+y;
15         case '-': return x-y;
16         case '*': return x*y;
17         case '/': return x/y;
18         case '#': return y-x;
19         case '$': return y/x;
20     }
21 }
22 
23 double pd1 ()
24 {
25     double x=cal((double)nums[0],(double)nums[1],ops[0]);
26     double y=cal(x,(double)nums[2],ops[1]);
27     return cal(y,(double)nums[3],ops[2]);
28 
29 }
30 
31 double pd2 ()
32 {
33     double x=cal((double)nums[0],(double)nums[1],ops[0]);
34     double y=cal((double)nums[2],(double)nums[3],ops[2]);
35     return cal(x,y,ops[1]);
36 
37 
38 }
39 
40 int  makeops(int dep)
41 {
42     if(dep==3)
43     {
44         if(fabs(pd1()-24.0)<1.0e-2) return 1; //¾ø¶ÔÖµ
45         if(fabs(pd2()-24.0)<1.0e-2) return 1;
46         return 0;
47     }
48 
49         for(int i=0;i<6;i++)
50         {
51             ops[dep]=op[i];
52             if(makeops(dep+1)) return 1;
53         }
54         return 0;
55 
56 }
57 
58 
59 int makenums(int dep)
60 {
61     if(dep==4) {
62         return makeops(0);
63     }
64         for(int i=0;i<4;i++)
65         {
66             if(!vis[i])
67             {
68                 nums[dep]=num[i];
69                 vis[i]=1;
70                 if(makenums(dep+1)) return 1;
71                 vis[i]=0;
72             }
73 
74          }
75      return 0;
76 }
77 
78 int main()
79 {
80     int t;
81     scanf("%d",&t);
82     while(t--){
83             memset(vis,0,sizeof(vis));
84         for(int i=0;i<4;i++)
85             scanf("%d",&num[i]);
86         if(makenums(0)) puts("Yes");
87         else puts("No");
88     }
89     return 0;
90 }
原文地址:https://www.cnblogs.com/yijiull/p/6637834.html