永发信息网

pascal做21点或者24点游戏

答案:2  悬赏:60  手机版
解决时间 2021-04-25 04:08
  • 提问者网友:玫瑰园
  • 2021-04-24 19:39
pascal做21点或者24点游戏
最佳答案
  • 五星知识达人网友:污到你湿
  • 2021-04-24 20:37

【算法分析】


计算24点(或21点)主要应用四种运算.开始状态有四个操作数,一个运算符对应两个操作数,


所以一开始选择两个操作数分别对四个操作符进行循环检测,


每一次运算后产生了新的数,两个数运算变成一个数,整体是少了一个操作数,所以四个数最终是三次运算。由于操作的层数比较少(只有三层),


所以可以用回溯的算法来进行检测,当找到一个解时便结束查找。如果所有的情况都找过后仍然没有,则输出无解的信息。


rogram point24(input,output);
type arr=array [1..4] of integer;
var i,result,n,len:integer;
d:arr;
r:array [1..3,1..4] of integer;
infile,outfile:text;
procedure print;
var i,j:integer;
begin
assign(outfile,'point24.out');rewrite(outfile);
for i:=1 to 3 do
begin
for j:=1 to 3 do
if j<>2 then write(outfile,r[i,j])
else case r[i,j] of
1:write(outfile,'+');
2:write(outfile,'-');
3:write(outfile,'*');
4:write(outfile,'/')
end;
end;
close(outfile);
end;


procedure try(k:integer;d:arr);
var a,b,i,j,l,t:integer;
e:arr;
begin
if k=1 then
if d[1]=24 then begin
print;
halt
end
else
else begin
for i:=1 to k-1 do
for j:=i+1 to k do
begin
a:=d[i]; b:=d[j];
if a<b then begin t:=a;a:=b;b:=t end;
t:=0;
for l:=1 to k do if (l<>i) and (l<>j)
then begin t:=t+1;e[t]:=d[l] end;
r[5-k,1]:=a;
r[5-k,3]:=b;
r[5-k,4]:=-1;
for l:=1 to 4 do
begin
case l of
1:r[5-k,4]:=a+b;
2:r[5-k,4]:=a-b;
3:r[5-k,4]:=a*b;
4:if b<>0
then if a mod b=0 then r[5-k,4]:=a div b
end;
r[5-k,2]:=l;
if r[5-k,4]<>-1
then begin
e[t+1]:=r[5-k,4];
try(k-1,e)
end
end
end
end;
end;
begin
assign(infile,'point24.in');reset(infile);
for i:=1 to 4 do read(infile,d[i]);
close(infile);
try(4,d);
assign(outfile,'point24.out');
rewrite(outfile);
writeln(outfile,'No answer!');
close(outfile);
end.


全部回答
  • 1楼网友:西岸风
  • 2021-04-24 22:02

Program blackjack(input,output); VAR

uses crt; label 1,2; type nbt=array [-1..5] of 0..24; var com,ply:nbt; ci,pi,x,y,m,t:integer; procedure printnbt(a:nbt); var i:integer; begin for i:=1 to 5 do case a[i] of 0:; 1:write('A'); 11:write('J'); 12:write('Q'); 13:write('K'); else write(a[i]); end; writeln; end; procedure checknbt(a:nbt); var i:integer;k:nbt; begin for i:=1 to 5 do case a[i] of 1:k[i]:=11; 11,12,13:k[i]:=10; else k[i]:=a[i]; end; a[0]:=0; for i:=1 to 5 do a[0]:=a[0]+k[i]; for i:=1 to 5 do if (a[0]>21) and (k[i]=11) then a[0]:=a[0]-10; end; procedure takecard(a:nbt); begin clrscr; a[a[-1]]:=random(13)+1; checknbt(a); printnbt(ply); printnbt(com); inc(a[-1]) end; begin randomize; m:=10000; 1: writeln(m); for t:=0 to 5 do begin com[t]:=0; ply[t]:=0; end; com[-1]:=1; ply[-1]:=1; read(y); if y>m then y:=m; takecard(ply); takecard(ply); takecard(com); read(x); while (x=1) and (ply[-1]<=5) do begin takecard(ply); if ply[0]>21 then begin writeln('you lose!'); m:=m-y; goto 2; end; read(x); end; repeat takecard(com); if com[0]>21 then begin writeln('you win!'); m:=m+y; goto 2; end; until (com[-1]>5) or (com[0]>17); if ply[0]>com[0] then begin writeln('you win!'); m:=m+y; end else if ply[0]<com[0] then begin writeln('you lose!'); m:=m-y; end else writeln('Duece!'); 2: writeln('continue?'); read(x); if x=1 then goto 1; end.

语言环境 FREE PASCAL

资源来自:百度知道 fp_Fzking

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯