求翻译....c→pascal
答案:2 悬赏:50 手机版
解决时间 2021-11-26 13:39
- 提问者网友:蓝莓格格巫
- 2021-11-26 05:10
求翻译....c→pascal
最佳答案
- 五星知识达人网友:枭雄戏美人
- 2021-11-26 06:35
严格来说是用C++编写的,不可能全部用pascal编写,只能采用等价的写法
希望采纳
1.
const
maxn=62;
type
arr2=array[0..5,0..maxn] of integer;
var
f,h,weight:array[0..maxn,0..maxn] of integer;
x,y:arr2;
i,a,b,ca,n,path,c:integer;
g:array[0..maxn,0..maxn] of boolean;
procedure memmove(var dest:arr2;src:arr2;size_t:longint);
var
i:longint;
begin
for i:=0 to size_t do dest[i]:=src[i];
end;
procedure update(var a:integer;b:integer);
begin
if b>a then a:=b;
end;
procedure cal(r,fa:integer);
var
i,j,k,l:integer;
begin
for i:=1 to n do
if (i<>0)<>((fa<>0) and g[i,r]) then cal(i,r);
fillchar(x,sizeof(x),0);
for i:=1 to n do
begin
if (i<>0)=((fa<>0) or not(g[i,r])) then continue;
memmove(y,x,sizeof(x));
for j:=0 to 2 do
for k:=0 to path do
for l:=0 to path-k do
begin
update(y[j,k+l],x[j,k]+f[i,l]);
if j<2 then
update(y[j+1,k+l],x[j,k]+h[i,l]+weight[i,r]);
end;
memmove(x,y,sizeof(x));
end;
for i:=0 to path do
begin
h[r,i]:=x[1,i];
f[r,i]:=x[0,i];
if i<>0 then
begin
update(f[r,i],x[1,i-1]);
update(f[r,i],x[2,i-1]);
end;
end;
end;
begin
assign(input,'path.in');reset(input);
assign(output,'path.out');rewrite(output);
readln(n,path);
fillchar(g,sizeof(g),0);
for i:=1 to n-1 do
begin
readln(a,b,c);
weight[a,b]:=c;
weight[b,a]:=weight[a,b];
g[b,a]:=true;
g[a,b]:=g[b,a];
end;
fillchar(f,sizeof(f),0);
fillchar(h,sizeof(h),0);
cal(1,-1);
writeln(f[1,path]);
close(input);close(output);
end.
2.
const
maxn=32;
maxv=102;
base=maxn*maxv;
type
state=record
x:array[0..base*2+1] of boolean;
mi,ma:integer;
end;
var
value:array[0..maxn] of integer;
i,n,ans:integer;
type_:array[0..maxn] of char;
u:array[0..maxn,0..maxn] of boolean;
f:array[0..maxn,0..maxn] of state;
s:string;
function min(a,b:integer):integer;
begin
if a>b then min:=b else min:=a;
end;
function max(a,b:integer):integer;
begin
if a>b then max:=a else max:=b;
end;
function sign(r:integer):integer;
begin
if r>=1 then sign:=-1 else sign:=1;
end;
function merge(a,b:state):state;
var
i:integer;
begin
a.mi:=min(a.mi,b.mi);
a.ma:=max(a.ma,b.ma);
for i:=a.mi to a.ma do a.x[i+base]:=a.x[i+base] or b.x[i+base];
exit(a);
end;
function move(a:state;k:integer):state;
var
i:integer;
b:state;
begin
b.mi:=a.mi+k;
b.ma:=a.ma+k;
b.x[base]:=false;
for i:=a.mi to a.ma do b.x[i+k+base]:=a.x[i+base];
exit(b);
end;
procedure cal(a,b:integer);
var
i:integer;
begin
if u[a,b] then exit;
u[a,b]:=true;
if a>n then
with f[a,b] do
begin
fillchar(x,sizeof(x),0);
ma:=0;
mi:=ma;
x[base]:=true;
end
else
begin
cal(a+1,b);
f[a,b]:=move(f[a+1,b],value[a]*sign(b));
for i:=1 to b do
begin
cal(a+1,b-i);
f[a,b]:=merge(f[a,b],move(f[a+1,b-i],value[a]*sign(b)));
end;
if type_[a]='-' then
begin
cal(a+1,b+1);
f[a,b]:=merge(f[a,b],move(f[a+1,b+1],value[a]*sign(b)));
end;
end;
end;
begin
assign(input,'parentheses.in');reset(input);
assign(output,'parentheses.out');rewrite(output);
readln(s);
read(value[1]);
type_[1]:='+';
read(type_[n+1]);
while (type_[n+1]<>'')and(type_[n+1]<>' ')and(type_[n+1]<>chr(13)) do
begin
inc(n);
read(value[n]);
read(type_[n+1]);
end;
fillchar(u,sizeof(u),0);
for i:=1 to n do
if type_[i]='-' then value[i]:=value[i]*-1;
cal(1,0);
ans:=0;
for i:=f[1,0].mi to f[1,0].ma do
if f[1,0].x[base+1] then inc(ans);
writeln(ans);
close(input);close(output);
end.
以上程序已在fpc中编译通过
原创内容,严禁抄袭
希望采纳
1.
const
maxn=62;
type
arr2=array[0..5,0..maxn] of integer;
var
f,h,weight:array[0..maxn,0..maxn] of integer;
x,y:arr2;
i,a,b,ca,n,path,c:integer;
g:array[0..maxn,0..maxn] of boolean;
procedure memmove(var dest:arr2;src:arr2;size_t:longint);
var
i:longint;
begin
for i:=0 to size_t do dest[i]:=src[i];
end;
procedure update(var a:integer;b:integer);
begin
if b>a then a:=b;
end;
procedure cal(r,fa:integer);
var
i,j,k,l:integer;
begin
for i:=1 to n do
if (i<>0)<>((fa<>0) and g[i,r]) then cal(i,r);
fillchar(x,sizeof(x),0);
for i:=1 to n do
begin
if (i<>0)=((fa<>0) or not(g[i,r])) then continue;
memmove(y,x,sizeof(x));
for j:=0 to 2 do
for k:=0 to path do
for l:=0 to path-k do
begin
update(y[j,k+l],x[j,k]+f[i,l]);
if j<2 then
update(y[j+1,k+l],x[j,k]+h[i,l]+weight[i,r]);
end;
memmove(x,y,sizeof(x));
end;
for i:=0 to path do
begin
h[r,i]:=x[1,i];
f[r,i]:=x[0,i];
if i<>0 then
begin
update(f[r,i],x[1,i-1]);
update(f[r,i],x[2,i-1]);
end;
end;
end;
begin
assign(input,'path.in');reset(input);
assign(output,'path.out');rewrite(output);
readln(n,path);
fillchar(g,sizeof(g),0);
for i:=1 to n-1 do
begin
readln(a,b,c);
weight[a,b]:=c;
weight[b,a]:=weight[a,b];
g[b,a]:=true;
g[a,b]:=g[b,a];
end;
fillchar(f,sizeof(f),0);
fillchar(h,sizeof(h),0);
cal(1,-1);
writeln(f[1,path]);
close(input);close(output);
end.
2.
const
maxn=32;
maxv=102;
base=maxn*maxv;
type
state=record
x:array[0..base*2+1] of boolean;
mi,ma:integer;
end;
var
value:array[0..maxn] of integer;
i,n,ans:integer;
type_:array[0..maxn] of char;
u:array[0..maxn,0..maxn] of boolean;
f:array[0..maxn,0..maxn] of state;
s:string;
function min(a,b:integer):integer;
begin
if a>b then min:=b else min:=a;
end;
function max(a,b:integer):integer;
begin
if a>b then max:=a else max:=b;
end;
function sign(r:integer):integer;
begin
if r>=1 then sign:=-1 else sign:=1;
end;
function merge(a,b:state):state;
var
i:integer;
begin
a.mi:=min(a.mi,b.mi);
a.ma:=max(a.ma,b.ma);
for i:=a.mi to a.ma do a.x[i+base]:=a.x[i+base] or b.x[i+base];
exit(a);
end;
function move(a:state;k:integer):state;
var
i:integer;
b:state;
begin
b.mi:=a.mi+k;
b.ma:=a.ma+k;
b.x[base]:=false;
for i:=a.mi to a.ma do b.x[i+k+base]:=a.x[i+base];
exit(b);
end;
procedure cal(a,b:integer);
var
i:integer;
begin
if u[a,b] then exit;
u[a,b]:=true;
if a>n then
with f[a,b] do
begin
fillchar(x,sizeof(x),0);
ma:=0;
mi:=ma;
x[base]:=true;
end
else
begin
cal(a+1,b);
f[a,b]:=move(f[a+1,b],value[a]*sign(b));
for i:=1 to b do
begin
cal(a+1,b-i);
f[a,b]:=merge(f[a,b],move(f[a+1,b-i],value[a]*sign(b)));
end;
if type_[a]='-' then
begin
cal(a+1,b+1);
f[a,b]:=merge(f[a,b],move(f[a+1,b+1],value[a]*sign(b)));
end;
end;
end;
begin
assign(input,'parentheses.in');reset(input);
assign(output,'parentheses.out');rewrite(output);
readln(s);
read(value[1]);
type_[1]:='+';
read(type_[n+1]);
while (type_[n+1]<>'')and(type_[n+1]<>' ')and(type_[n+1]<>chr(13)) do
begin
inc(n);
read(value[n]);
read(type_[n+1]);
end;
fillchar(u,sizeof(u),0);
for i:=1 to n do
if type_[i]='-' then value[i]:=value[i]*-1;
cal(1,0);
ans:=0;
for i:=f[1,0].mi to f[1,0].ma do
if f[1,0].x[base+1] then inc(ans);
writeln(ans);
close(input);close(output);
end.
以上程序已在fpc中编译通过
原创内容,严禁抄袭
全部回答
- 1楼网友:骨子里都是戏
- 2021-11-26 07:04
额 会p的不会c 会c的不会p 告诉我哪个题吧 我帮你写一遍
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯