Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MODULE msp1;
- FROM InOut IMPORT WriteString,WriteLn,WriteInt;
- FROM FIO IMPORT Open,Close,RdInt,File,EOF;
- CONST brojevi="brojevi.txt";
- N=9;
- TYPE NIZ = ARRAY [1..9] OF INTEGER;
- VAR niz:NIZ;
- (*PROCEDURE*)
- PROCEDURE uNiz(VAR niz:NIZ);
- VAR f:File;
- i:INTEGER;
- BEGIN
- f:=Open(brojevi);
- EOF:=FALSE;
- i:=1;
- WHILE NOT EOF DO
- niz[i]:=RdInt(f);
- INC(i);
- END;
- WriteString('--- Ispis brojeva iz liste: ---');
- WriteLn;
- FOR i:=1 TO 9 DO
- WriteInt(niz[i],1);
- WriteLn;
- END;
- Close(f);
- END uNiz;
- (*PRVI NACIN * BRUTE-FORCE 0(n^3) NAJSPORIJE*)
- PROCEDURE brute(broj:NIZ);
- VAR sum,max,od,do,i,d,g:INTEGER;
- BEGIN
- max:=0;
- FOR d:=1 TO N DO
- FOR g:=1 TO N DO
- sum:=0;
- FOR i:=d TO g DO
- sum:=sum+broj[i];
- END;
- IF sum>max THEN
- max:=sum;
- od:=d;
- do:=g;
- END;
- END;
- END;
- WriteLn;
- WriteString('--- BruteForce ---');
- WriteLn;
- WriteString('Maksimum je: ');
- WriteInt(max,1);
- WriteString(' u intervalu od ');
- WriteInt(od,1);
- WriteString(' do ');
- WriteInt(do,1);
- WriteLn;
- END brute;
- (*DRUGI NACIN * 0(n) NAJBRZE*)
- PROCEDURE nulaN(niz:NIZ);
- VAR maxDo,max,od,do,i,d,g:INTEGER;
- BEGIN
- max:=0;
- maxDo:=0;
- FOR i:=1 TO N DO
- IF maxDo=0 THEN
- d:=i;
- END;
- maxDo:=maxDo+niz[i];
- IF maxDo<0 THEN
- maxDo:=0;
- END;
- IF maxDo>max THEN
- od:=d;
- do:=i;
- max:=maxDo;
- END;
- END;
- WriteLn;
- WriteString('--- Najefikasniji nacin ---');
- WriteLn;
- WriteString('Maksimum je: ');
- WriteInt(max,1);
- WriteString(' u intervalu od ');
- WriteInt(od,1);
- WriteString(' do ');
- WriteInt(do,1);
- WriteLn;
- END nulaN;
- (*GLAVNI DIO PROGRAMA*)
- BEGIN
- uNiz(niz);
- brute(niz);
- nulaN(niz);
- END msp1.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement