PAT 2-08. 用扑克牌计算24点(25):

题目链接:http://www.patest.cn/contests/ds/2-08

解题思路:思路参考24点游戏技巧http://www.24game.com.cn/articles/points24-game-tips-grade6.html

     方法为:暴力枚举每次所选的数字和运算符的五种不同运算方式

五种不同运算方式如下(括号的五种不同组合):

  ((a1 op1 a2) op2 a3) op3 a4

  (a1 op1 (a2 op2 a3)) op3 a4

  (a1 op1 a2) op2 (a3 op3 a4)

  a1 op1 ((a2 op2 a3) op3 a4)

  a1 op1 (a2 op2 (a3 op3 a4))

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <cstdio> 
   
char op[5]= {'#','+','-','*','/',}; 
   
double cal(double x,double y,int op) 
    switch(op) 
    
    case 1: 
        return x+y; 
    case 2: 
        return x-y; 
    case 3: 
        return x*y; 
    case 4: 
        return x/y; 
    
   
double cal_m1(double i,double j,double k,double t,int op1,int op2,int op3) 
    double r1,r2,r3; 
    r1 = cal(i,j,op1); 
    r2 = cal(r1,k,op2); 
    r3 = cal(r2,t,op3); 
    return r3; 
   
double cal_m2(double i,double j,double k,double t,int op1,int op2,int op3) 
    double r1,r2,r3 ; 
    r1 = cal(i,j,op1); 
    r2 = cal(k,t,op3); 
    r3 = cal(r1,r2,op2); 
    return r3; 
   
double cal_m3(double i,double j,double k,double t,int op1,int op2,int op3) 
    double r1,r2,r3; 
    r1 = cal(j,k,op2); 
    r2 = cal(i,r1,op1); 
    r3 = cal(r2,t,op3); 
    return r3; 
   
double cal_m4(double i,double j,double k,double t,int op1,int op2,int op3) 
    double r1,r2,r3 ; 
    r1 = cal(k,t,op3); 
    r2 = cal(j,r1,op2); 
    r3 = cal(i,r2,op1); 
    return r3; 
   
double cal_m5(double i,double j,double k,double t,int op1,int op2,int op3) 
    double r1,r2,r3; 
    r1 = cal(j,k,op2); 
    r2 = cal(r1,t,op3); 
    r3 = cal(i,r2,op1); 
    return r3; 
   
int get_24(int i,int j,int k,int t) 
    for(int op1 = 1; op1 <= 4; op1++) 
    
        for(int op2 = 1; op2 <= 4; op2++) 
        
            for(int op3 = 1; op3 <= 4; op3++) 
            
                if(cal_m1(i,j,k,t,op1,op2,op3) == 24) 
                
                    printf("((%d%c%d)%c%d)%c%d\n",i,op[op1],j,op[op2],k,op[op3],t); 
                    return 1; 
                
                if(cal_m2(i,j,k,t,op1,op2,op3) == 24) 
                
                    printf("(%d%c%d)%c(%d%c%d)\n",i,op[op1],j,op[op2],k,op[op3],t); 
                    return 1; 
                
                if(cal_m3(i,j,k,t,op1,op2,op3) == 24) 
                
                    printf("(%d%c(%d%c%d))%c%d\n",i,op[op1],j,op[op2],k,op[op3],t); 
                    return 1; 
                
                if(cal_m4(i,j,k,t,op1,op2,op3) == 24) 
                
                    printf("%d%c(%d%c(%d%c%d))\n",i,op[op1],j,op[op2],k,op[op3],t); 
                    return 1; 
                
                if(cal_m5(i,j,k,t,op1,op2,op3) == 24) 
                
                    printf("%d%c((%d%c%d)%c%d)\n",i,op[op1],j,op[op2],k,op[op3],t); 
                    return 1; 
                
   
            
        
    
    return 0; 
   
int main() 
    int a[4]; 
    int t1, t2, t3, t4; 
    int flag; 
    for(int i = 0; i < 4; i++) 
        scanf("%d",&a[i]); 
    for(int i = 0; i < 4; i++) 
    
        for(int j = 0; j < 4; j++) 
        
            if(j==i) 
                continue
            for(int k = 0; k < 4; k++) 
            
                if(i==k||j==k) 
                    continue
                for(int t = 0; t < 4; t++) 
                
                    if(t==i||t==j||t==k) 
                        continue
                    t1 = a[i], t2= a[j], t3= a[k], t4= a[t]; 
   
                    flag = get_24(t1,t2,t3,t4); 
                    if(flag ==1) 
                        break
                
                if(flag == 1) 
                    break
            
            if(flag == 1) 
                break
        
        if(flag == 1) 
            break
    
    if(flag == 0) 
        printf("-1\n"); 
   
    return 0; 

  

暴力枚举代码转载自:http://blog.csdn.net/u012860063/article/details/40435363

  

  

 

          

posted @   职场亮哥  阅读(291)  评论(0编辑  收藏  举报
编辑推荐:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示