Advertisement
ridjis

max suma pb

Mar 23rd, 2014
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. MODULE msp1;
  2. FROM InOut IMPORT WriteString,WriteLn,WriteInt;
  3. FROM FIO IMPORT Open,Close,RdInt,File,EOF;
  4.  
  5. CONST brojevi="brojevi.txt";
  6.       N=9;
  7. TYPE NIZ = ARRAY [1..9] OF INTEGER;
  8. VAR niz:NIZ;
  9. (*PROCEDURE*)
  10. PROCEDURE uNiz(VAR niz:NIZ);
  11. VAR f:File;
  12.     i:INTEGER;
  13. BEGIN
  14.   f:=Open(brojevi);
  15.   EOF:=FALSE;
  16.   i:=1;
  17.   WHILE NOT EOF DO
  18.     niz[i]:=RdInt(f);
  19.     INC(i);
  20.   END;
  21.  
  22.   WriteString('--- Ispis brojeva iz liste: ---');
  23.   WriteLn;
  24.   FOR i:=1 TO 9 DO
  25.     WriteInt(niz[i],1);
  26.     WriteLn;
  27.   END;
  28.   Close(f);
  29. END uNiz;
  30.  
  31. (*PRVI NACIN * BRUTE-FORCE 0(n^3) NAJSPORIJE*)
  32. PROCEDURE brute(broj:NIZ);
  33. VAR sum,max,od,do,i,d,g:INTEGER;
  34. BEGIN
  35.   max:=0;
  36.   FOR d:=1 TO N DO
  37.     FOR g:=1 TO N DO
  38.       sum:=0;
  39.       FOR i:=d TO g DO
  40.         sum:=sum+broj[i];
  41.       END;
  42.       IF sum>max THEN
  43.         max:=sum;
  44.     od:=d;
  45.     do:=g;
  46.       END;
  47.     END;
  48.   END;
  49.   WriteLn;
  50.   WriteString('--- BruteForce ---');
  51.   WriteLn;
  52.   WriteString('Maksimum je: ');
  53.   WriteInt(max,1);
  54.   WriteString(' u intervalu od ');
  55.   WriteInt(od,1);
  56.   WriteString(' do ');
  57.   WriteInt(do,1);
  58.   WriteLn;
  59. END brute;
  60. (*DRUGI NACIN * 0(n) NAJBRZE*)
  61. PROCEDURE nulaN(niz:NIZ);
  62. VAR maxDo,max,od,do,i,d,g:INTEGER;
  63. BEGIN
  64.   max:=0;
  65.   maxDo:=0;
  66.   FOR i:=1 TO N DO
  67.     IF maxDo=0 THEN
  68.       d:=i;
  69.     END;
  70.     maxDo:=maxDo+niz[i];
  71.     IF maxDo<0 THEN
  72.       maxDo:=0;
  73.     END;
  74.     IF maxDo>max THEN
  75.       od:=d;
  76.       do:=i;
  77.       max:=maxDo;
  78.     END;
  79.   END;
  80.   WriteLn;
  81.   WriteString('--- Najefikasniji nacin ---');
  82.   WriteLn;
  83.   WriteString('Maksimum je: ');
  84.   WriteInt(max,1);
  85.   WriteString(' u intervalu od ');
  86.   WriteInt(od,1);
  87.   WriteString(' do ');
  88.   WriteInt(do,1);
  89.   WriteLn;
  90. END nulaN;
  91.  
  92.  
  93. (*GLAVNI DIO PROGRAMA*)
  94. BEGIN
  95.   uNiz(niz);
  96.   brute(niz);
  97.   nulaN(niz);
  98. END msp1.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement