24点游戏

题目描述

   24点游戏是一个非常有意思的游戏,很流行,玩法很简单:给你4张牌,每张牌上有数字(其中A代表1,J代表11,Q代表12,K代表13),你可以利用数学中的加、减、乘、除以及括号想办法得到24,例如:


((A*K)-J)*Q等价于((1*13)-11)*12=24


   加减乘不用多说了,但除法必须满足能整除才能除!这样有一些是得不到24点的,所以这里只要求求出不超过24的最大值。

输入

   输入第一行N(1<=N<=5)表示有N组测试数据。每组测试数据输入4行,每行一个整数(1到13)表示牌值。


样例输入
3

3

3

3

3

1

1

1

1

12

5

13

1
样例输出
24

4

21
uses math;
var
        a,b,c,s1,s2,s3,s4,y1,y2,y3,ans,h,o,e:longint;
        v:array[1..4]of longint;
begin
        readln(a);
        for b:=1 to a do
        begin
                h:=0;
                ans:=0;
                readln(v[1]);
                readln(v[2]);
                readln(v[3]);
                readln(v[4]);
                for s1:=1 to 4 do
                begin
                        for y1:=1 to 4 do
                        begin
                                for s2:=1 to 4 do
                                begin
                                        if s1=s2 then continue;
                                        for y2:=1 to 4 do
                                        begin
                                                for s3:=1 to 4 do
                                                begin
                                                        if (s1=s3)or(s2=s3) then continue;
                                                        for y3:=1 to 4 do
                                                        begin
                                                                for s4:=1 to 4 do
                                                                begin
                                                                        if (s1=s4)or(s2=s4)or(s3=s4) then continue;
                                                                        ans:=0;
                                                                        if y1=1 then ans:=v[s1]+v[s2];
                                                                        if y1=2 then ans:=v[s1]-v[s2];
                                                                        if y1=3 then ans:=v[s1]*v[s2];
                                                                        if y1=4 then
                                                                                if (v[s2]<>0)and(v[s1] mod v[s2]=0) then
                                                                                        ans:=v[s1] div v[s2] else continue;
                                                                        if y2=1 then ans:=ans+v[s3];
                                                                        if y2=2 then ans:=ans-v[s3];
                                                                        if y2=3 then ans:=ans*v[s3];
                                                                        if y2=4 then
                                                                                if (v[s3]<>0)and(ans mod v[s3]=0) then
                                                                                        ans:=ans div v[s3] else continue;
                                                                        if y3=1 then ans:=ans+v[s4];
                                                                        if y3=2 then ans:=ans-v[s4];
                                                                        if y3=3 then ans:=ans*v[s4];
                                                                        if y3=4 then
                                                                                if (v[s4]<>0)and(ans mod v[s4]=0) then
                                                                                        ans:=ans div v[s4] else continue;
                                                                        if ans<=24 then h:=max(ans,h);
                                                                end;
                                                        end;
                                                end;
                                        end;
                                end;
                        end;
                end;
                for s1:=1 to 4 do
                begin
                        for y1:=1 to 4 do
                        begin
                                for s2:=1 to 4 do
                                begin
                                        if s1=s2 then continue;
                                        for y2:=1 to 4 do
                                        begin
                                                for s3:=1 to 4 do
                                                begin
                                                        if (s1=s3)or(s2=s3) then continue;
                                                        for y3:=1 to 4 do
                                                        begin
                                                                for s4:=1 to 4 do
                                                                begin
                                                                        if (s1=s4)or(s2=s4)or(s3=s4) then continue;
                                                                        ans:=0;
                                                                        if y1=1 then o:=v[s1]+v[s2];
                                                                        if y1=2 then o:=v[s1]-v[s2];
                                                                        if y1=3 then o:=v[s1]*v[s2];
                                                                        if y1=4 then
                                                                                if (v[s2]<>0)and(v[s1] mod v[s2]=0) then
                                                                                        o:=v[s1] div v[s2] else continue;
                                                                        if y2=1 then e:=v[s3]+v[s4];
                                                                        if y2=2 then e:=v[s3]-v[s4];
                                                                        if y2=3 then e:=v[s3]*v[s4];
                                                                        if y2=4 then
                                                                                if (v[s4]<>0)and(v[s3] mod v[s4]=0) then
                                                                                        e:=v[s3] div v[s4] else continue;
                                                                        if y3=1 then ans:=o+e;
                                                                        if y3=2 then ans:=o-e;
                                                                        if y3=3 then ans:=o*e;
                                                                        if y3=4 then
                                                                                if (e<>0)and(o mod e=0) then
                                                                                        ans:=o div e else continue;
                                                                        if ans<=24 then h:=max(ans,h);
                                                                end;
                                                        end;
                                                end;
                                        end;
                                end;
                        end;
                end;
                writeln(h);
        end;
end.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值