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))

代码如下:

#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

  

  

 

          

转载于:https://www.cnblogs.com/CHLL55/p/4328365.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值