您的位置:首页 > 其它

最大连续数列的和

2017-03-09 16:01 246 查看
最大连续数列的和
Time Limit:1000MS  Memory Limit:65536K

Total Submit:142 Accepted:83
Description

 求最大连续子序列的和

Input

 第一行输入n(n<=500),第二行为n个以空格分开的整数(-1000到1000之间);

Output

  该序列中最大的连续子序列的和

Sample Input


 6
  1 2 -5 6 7 8


Sample Output


  21

var
a,f:array[0..1000] of longint;
n,m,j,i,max:longint;
begin
readln(n);
for i:=1 to n do
read(a[i]);
for i:=1 to n do
begin
if (a[i]+f[i-1]>0) and (f[i-1]<>0) then f[i]:=a[i]+f[i-1];
if (a[i]+f[i-1]<0) and (f[i-1]<>0) then f[i]:=0;
if f[i-1]=0 then if a[i]>0 then f[i]:=a[i]
else f[i]:=0;
end;
for i:=1 to n do
if f[i]>max then max:=f[i];
write(max);
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: