今有一堆火柴,两人依次从中拿取,规定每次只能从一堆中取若干根, 拿走最后一根的获胜。用pascal语言做出来
- 提问者网友:雾里闻花香
- 2021-02-04 01:16
或者说有50根火柴,每人最多拿5跟,谁最后没的拿就输了。用pascal语言编程,求高手帮忙,感谢了!!!!
- 五星知识达人网友:你可爱的野爹
- 2021-02-04 01:53
每次观察一下剩几根,可以控制除第一次外,每次两个人恰好拿1+5=6根。
只要第一次控制剩余的根数,除以6余0即可
- 1楼网友:毛毛
- 2021-02-04 03:14
type st=array[0..30] of longint;
var ans:st;f:array[1..80,1..80] of st;a:array[1..80] of longint;
m,n:longint;
procedure add(var x:st;y,z:st);
var temp,i:longint;
begin
temp:=0;
if y[0]>z[0] then x[0]:=y[0] else x[0]:=z[0];
for i:=1 to x[0] do
begin
temp:=y[i]+z[i]+temp;
x[i]:=temp mod 10000;
temp:=temp div 10000;
end;
if temp>0 then begin inc(x[0]); x[x[0]]:=temp;end;
end;
procedure mul(var x:st);
var temp,i:longint;
begin
temp:=0;
for i:=1 to x[0] do
begin
temp:=x[i]*2+temp;
x[i]:=temp mod 10000;
temp:=temp div 10000;
end;
while temp>0 do begin inc(x[0]);x[x[0]]:=temp mod 10000;temp:=temp div 10000;end;
end;
procedure plus(var x:st;y:st;z:longint);
var i:longint;
begin
x:=y;inc(x[1],z);i:=1;
while x[i]>=10000 do
begin
x[i+1]:=x[i] div 10000+x[i+1];
x[i]:=x[i] mod 10000;
inc(i);
if x[0]<i then x[0]:=i;
end;
end;
function compare(a,b:st):boolean;
var i:longint;
begin
if a[0]>b[0] then exit(true);
if a[0]<b[0] then exit(false);
for i:=a[0] downto 1 do
if a[i]>b[i] then exit(true)
else if a[i]<b[i] then exit(false);
exit(false);
end;
procedure dp;
var i,j:longint;max,min:st;
begin
fillchar(f,sizeof(f),0);
for i:=1 to m do begin f[i,i][0]:=1;f[i,i][1]:=a[i]*2;end;
for i:=m-1 downto 1 do
for j:=i+1 to m do
begin
plus(max,f[i+1,j],a[i]);
mul(max);
plus(min,f[i,j-1],a[j]);
mul(min);
if compare(min,max) then max:=min;
f[i,j]:=max;
end;
add(ans,ans,f[1,m]);
end;
procedure re;
var i,j:longint;
begin
readln(n,m);
for i:=1 to n do
begin
for j:=1 to m do
read(a[j]);
readln;
dp;
end;
end;
procedure print;
var i:longint;
begin
write(ans[ans[0]]);
for i:=ans[0]-1 downto 1 do
begin
write(ans[i] div 1000 mod 10);
write(ans[i] div 100 mod 10);
write(ans[i] div 10 mod 10);
write(ans[i] mod 10);
end;
end;
begin
re;print;
end.
高精度也给粘上了,不高兴删了,dp的问题,将就着看吧