Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //maksimalna suma <proizvoljnog> broja susednih elemenata u nizu celih brojeva
- MODULE zadatakkonacnoresenje;
- FROM FIO IMPORT WrInt, WrLn, Create, File, Close;
- FROM InOut IMPORT ReadInt, WriteInt, WriteLn, WriteString, WriteCard, ReadCard;
- CONST
- N = 5;
- TYPE
- interval = [1..N];
- VAR
- max, suma : INTEGER;
- d, g, od, do : interval;
- i, unos : CARDINAL;
- x : ARRAY[1..N] OF INTEGER;
- f : File;
- PROCEDURE maxsum1(niz: ARRAY OF INTEGER; VAR odd, doo : interval):INTEGER;
- VAR
- d, g : interval;
- i : CARDINAL;
- BEGIN
- max := 0;
- FOR d := 1 TO N DO
- FOR g := 1 TO N DO
- suma := 0;
- FOR i := d TO g DO
- suma := suma + niz[i-1]
- END;
- IF suma > max THEN
- max := suma;
- odd := d;
- doo := g
- END
- END
- END;
- RETURN max;
- END maxsum1;
- PROCEDURE maxsum2(niz: ARRAY OF INTEGER; VAR odd, doo : interval):INTEGER;
- VAR
- d, g : interval;
- i : CARDINAL;
- BEGIN
- max := 0;
- FOR d := 1 TO N DO
- suma := 0;
- FOR g := d TO N DO
- suma := suma + niz[g-1];
- IF suma > max THEN
- max := suma;
- odd := d;
- doo := g
- END
- END
- END;
- RETURN max;
- END maxsum2;
- PROCEDURE maxsum3(niz: ARRAY OF INTEGER; VAR odd, doo : interval):INTEGER;
- VAR
- d, g : CARDINAL;
- i : CARDINAL;
- pom : ARRAY [0..N] OF INTEGER;
- BEGIN
- pom[0] := 0;
- FOR i := 1 TO N DO
- pom[i] := pom[i-1] + niz[i-1]
- END;
- max := 0;
- FOR d := 1 TO N DO
- FOR g := 1 TO N DO
- suma := pom[g] - pom[d-1];
- IF suma > max THEN
- max := suma;
- odd := d;
- doo := g
- END
- END
- END;
- RETURN max;
- END maxsum3;
- BEGIN
- {//prvi put kriejtujes i upisujes i brojevi N, posle kad jednom napravis
- //malo sredi ovih par redova da izvlacis informacije iz vec gotovog i dodajes ih
- //nizu;
- }
- WriteString('Unesite niz x: ');
- WriteLn;
- f := Create('BrojeviN');
- FOR i := 1 TO N DO
- ReadInt(x[i]);
- WrInt(f, x[i], 0);
- WrLn(f);
- WriteLn;
- END;
- Close(f);
- REPEAT
- WriteLn;
- WriteString('Odaberi brzu, sporiju ili naprednu proceduru');
- WriteLn;
- WriteString('uneti 1, 2 ili 3 :');
- ReadCard(unos);
- UNTIL (unos = 1) OR (unos = 2) OR (unos = 3);
- CASE unos OF
- 1: max := maxsum1(x, od, do) |
- 2: max := maxsum2(x, od, do) |
- 3: max := maxsum3(x, od, do)
- ELSE
- WriteString('Greska prvog reda!')
- END;
- WriteLn;
- WriteString('Maksimum je');
- WriteInt(max,3);
- WriteString(' u intervalu od ');
- WriteCard(od,3);
- WriteString(' do ');
- WriteCard(do,3)
- END zadatakkonacnoresenje.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement