最大连续数列的和

    xiaoxiao2021-03-25  80

    最大连续数列的和

    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.

    转载请注明原文地址: https://ju.6miu.com/read-12584.html

    最新回复(0)