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
    评论
二十四点游戏是一款很受欢迎的数学游戏,玩家需要通过四则运算将给定的四个数字计算出24这个结果。在Android平台上开发这个游戏需要以下几个步骤。 首先,我们需要设计游戏界面。可以利用Android Studio提供的布局工具来设计一个简单直观的界面,包含四个数字输入框和一个计算按钮。玩家可以通过输入数字或者点击按钮来提交答案。 接下来,我们需要编写逻辑代码来实现游戏的规则。当玩家点击计算按钮时,我们首先需要获取四个输入框中的数字,并进行合法性检查。如果数字无效,则提示玩家重新输入。如果数字有效,则通过四则运算计算出所有可能的结果,并判断是否存在结果等于24的计算过程。如果存在结果等于24的计算过程,则提示玩家答案正确;否则,提示答案错误。 在实现四则运算的计算过程时,可以利用递归算法来穷举所有可能的组合。我们可以先从给定的四个数字中选择两个进行运算,得到一个新的数字,并将这个新的数字与另外两个数字结合进行运算,再得到一个结果。然后,将这个结果与另外两个数字进行运算,再得到一个新的结果。最后,将这个新的结果与剩下的一个数字进行运算,得到最终结果。通过递归算法,可以穷举所有可能的计算过程。 最后,我们需要添加一些额外的功能来增加游戏的趣味性。例如,可以添加计时器来记录玩家完成游戏所用的时间;也可以添加提示功能,当玩家无法找到答案时,可以点击提示按钮来获取一种可能的计算过程。这些额外功能可以通过Android的控件和事件处理来实现。 通过以上步骤,我们可以开发出一款可以玩家在Android设备上玩的二十四点游戏。玩家可以通过输入数字和进行四则运算来找到计算结果等于24的过程,锻炼数学思维和逻辑能力。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值