Bài 2: Cho dãy số gồm n số nguyên. Tìm dãy con có tổng lớn nhất
2 câu trả lời
Const Fi='DAYCON.INP';
Fo='DAYCON.OUT';
Type mang=array[1..1000000] of longint;
Var f,g: text;
A: mang;
n,i,j,dau,dai,max: longint;
Function Tong(A: mang; m,l: longint): longint;
Var tg,i: longint;
Begin
tg:=0;
For i:=m to m+1 do tg:=tg+A[i];
Tong:=tg;
End;
Begin
Assign(f,Fi); Reset(f);
Assign(g,Fo); Rewrite(g);
Readln(f,n);
For i:=1 to n do
Read(f,A[i]);
max:=A[1];
For i:=1 to n do
For j:=0 to n-i+1 do
If Tong(A,i,j)>max then
Begin
dau:=i;
max:=Tong(A,i,j);
dai:=j+1;
End;
For i:=dau to dau+dai do Write(g,A[i],' ');
Close(f);
Close(g);
End.
program bai_giai;
uses crt;
var A:array[1..10000] of integer;
n,i,j,t,vtd,vtc,max:integer;
begin
clrscr;
readln(n);
for i:=1 to n do read(A[i]);
max:=A[1]; vtd:=1; vtc:=1;
for i:=1 to n do
begin
t:=0;
for j:=i to n do
begin
t:=t+A[j];
if t>max then
begin
max:=t; vtd:=i; vtc:=j;
end;
end;
end;
for i:=vtd to vtc do write(A[i],' ');
readln; readln;
end.