您的位置:首页 > 其它

24点游戏

2016-10-06 13:52 417 查看


题目描述

   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;

4000
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: