iİnformatikanın məktəbdə tədrisi

7-9r

Найдите максимальную сумму подряд идущих элементов последовательности.

Понять этот алгоритм не так уж и просто. Но если сумеете понять, 
Вас ждет неимоверное наслаждение. 
Понять один раз самому - намного ценнее 100 пояснений.
Но, пояснения скоро будут.


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

Program A7_09;

Var

 f:text;

 a,maxs,s:integer;

 BEGIN

  assign(f,'in7_09.txt'); reset(f);

  if IoResult<>0 then

   begin writeln('Net faila'); readln;

   exit; end;

   readln(f,s);

   maxs:=s;

   while(not EOF(f)) do

     begin

       readln(f,a);

       if (s<0) then s:=a

                else s:=s+a;

       if (s>maxs) then maxs:=s;

     end;

   close(f);

   writeln('maxs=',maxs);

   readln;

 END.

// Program A7_09;

#include <iostream>

#include <fstream>

 

using namespace std;

 

int main()

{  ifstream f;

int a, maxs,s;

    f.open("in7_09.txt");

    if (!f) { cout<<"net faila\n"; return -1;}

    f>>s;

    maxs=s;

    while(f>>a)

    {  if (s<0)s=a ;   else s=s+a;

       if (s>maxs)maxs=s;

     }

     f.close();

     cout<<"maxs="<<maxs<<endl;

    return 0;

}