POJ上的重题还真多
Program P2593;
var
n,i,j,m,m2,p:longint;
a,b,c:array[0..100000] of longint;
begin
read(n);
a[0]:=-100001;
while (n>0) do
begin
j:=0;
m:=0;
for i:=1 to n do
begin
read(a[i]);
if a[i]<0 then inc(j);
if a[i]>a[m] then m:=i;
end;
if j>n-2 then
begin
p:=a[m];
m2:=0;
for i:=1 to n do if (i<>m) and (a[m2]<a[i]) then m2:=i;
inc(p,a[m2]);
writeln(p);
end
else
begin
p:=0;
m:=-100001;
for i:=1 to n do
begin
inc(p,a[i]);
if (p<0) then p:=0;
if m<p then m:=p;
b[i]:=m;
end;
p:=0;
m:=-100001;
for i:=n downto 1 do
begin
inc(p,a[i]);
if (p<0) then p:=0;
if m<p then m:=p;
c[i]:=m;
end;
m:=0;
for i:=1 to n-1 do
if m<b[i]+c[i+1] then m:=b[i]+c[i+1];
writeln(m);
end;
read(n);
end;
end.
本文提供了一段针对POJ上某一题目(P2593)的程序代码解析,该程序使用Pascal语言实现,主要解决的问题是如何找到一组整数中最大的连续子数组之和,同时考虑了所有元素为负数的特殊情况。
1007

被折叠的 条评论
为什么被折叠?



